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

前端 未结 4 586
小鲜肉
小鲜肉 2021-01-25 21:20

How can I check if User which came to my web site, is same User which came half hour before?

4条回答
  •  北海茫月
    2021-01-25 22:13

    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(affffdate(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!

提交回复
热议问题