I created simple hit counter that sends ajax requests to determine how long visitor is viewing the page.

In JS im just hitting PHP file every few seconds:

var ajaxreq = new XMLHttpRequest();
ajaxreq.open("GET", "visitlogger.php", true); 
ajaxreq.send();

PHP file (visitlogger.php) stores data in MySQL database. - stores "first hit timestamp" - stores HTTP_REFERER, user agent, IP etc. - updates "last hit timestamp" if session ID exists (to check how long user had page opened)

Now I need some cookie or something, to group visits by unique visitors.

How to do it?

有帮助吗?

解决方案

What I would do is save two cookies, first which will have small expiration and other as long say 4-6months, if the second cookie is set you know he ain't a unique visitor, but still drawbacks are:

1) if cookies disabled?

2)if cookies are cleared?

Edit code:

  if (!isset($_COOKIE["visit_id"])) { //short cookie

    //log in db

    setcookie("visit_id", session_id(), time()+60*60*1, "/"); //set cookie for a day 

  } 


  if (!isset($_COOKIE["visit_long"])) { //long cookie

    //log in db


    setcookie("visit_long", session_id(), time()+60*60*24*30*6, "/"); //set cookie long for 6 months

  } else {

    if (!isset($_COOKIE["visit_id"])) {

    //not a unique visitor


    setcookie("visit_id", session_id(), time()+60*60*1, "/"); //set cookie for one day

   }

 }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top