开发者

Check if same User visited web site!?! How?

How can I check if Us开发者_如何学运维er which came to my web site, is same User which came half hour before?


Drop a cookie with an expiration date. then check if it exists.


First you need to use his IP Address and then check if you have that IP saved in your database in lets say about last 24 hours and if you do than its the same user and if you dont then its someone else or his IP changed! ofc. there are much more things you can watch on but this is a basic.

ok here is an example (you just need to make database):

$ip=$_SERVER['REMOTE_ADDR'];
$cook=$_COOKIE["visitor"];
$checkit = mysql_query("SELECT * FROM pagevisitors WHERE date_format(date_added,'%Y%m%d%H')>date_format(adddate(now(), interval - 24 hour),'%Y%m%d%H') AND visitor_IP='".$ip."' AND visitor_Cookie='".$cook."' ORDER BY id DESC limit 1");
$checked = mysql_fetch_array($checkit);
if($checked){
  //same visitor
}
else{
  // new visitor & add him to your database & a cookie
  // make random string for him
  $hisString=genRandomString();
  setcookie("visitor", $hisString, time()+86400);
  mysql_query("INSERT INTO pagevisitors (visitor_IP, date_added,visitor_Cookie) VALUES  ('".$ip."', now(),'".$hisString."')");
}

// function for random string
function genRandomString(){
    $length = 25;
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    $string = '';    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}

and in database the field "id" make auto increase! :) this should work... didnt tested it!


What do u mean from the user ? Is the user logs in to your website or just lands on your web site?

1.you can get the ipAddress of the user but no guarantee. 2.Using cookies. How long you want to do the same process ? (Same user which came half hour before) so the expiration date of the cookie is important.


You could also try saving ip information in a database with a timestamp but since multiple users can be on the same ip it would be less accurate then going for a cookie like Victor suggested.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜