简体   繁体   中英

Page counter script. what is wrong?

i made this script that add in a mysql database information about who have visited my website.

session_start()

if(!isset($_SESSION['log'])) { 
   $ip=$_SERVER['REMOTE_ADDR'];
   $date=date("Y-m-d H:i:s");
   $browser=$_SERVER['HTTP_USER_AGENT'];
   $browser=mysql_real_escape_string($browser);
   if(isset($_SESSION['nickname'])) {
       $user=$_SESSION['nickname'];
   } else {
       $user="unknownABCD1234";
   }
   $insert=mysql_query("INSERT INTO views (ip, user, date, browser) VALUES ('$ip', '$user', '$date', '$browser')", $mydb);  
   $_SESSION['log']='logged';
   $_SESSION['iplog']=$ip;
   $_SESSION['datelog']=$date;
}

the problem is that it adds the referencee for each user many times (but not ever, just sometimes). example, i find on my db infos like these :

ID: 1
IP : 95.108.244.252
USER : unknownABCD1234 
DATE : 2010-08-07 01:16:00
BROWSER : Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)

ID : 2 
IP : 95.108.244.252 
USER : unknownABCD1234 
DATE : 2010-08-07 01:16:04
BROWSER : Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)

that's impossible no? i sure that the session during more than 4 seconds :) what happen?

The bot is discarding session info and ignoring your cookies.

This means that it shows up multiple times in your log. Yes, it does hit your site quickly and without session info, so you don't filter it out.

Your best option is to have some kind of duplicate filter for bots or IPs that hit quickly and repeatedly this way. You might also try adding explicit ignores to the few bot user agents that cause this problem with your script. By the time you have 10 or 15, you'll have dealt with the majority of the problem UAs.

Write session_start() at the start of code. (For more clarity in the question)

Although default session timeout is 1440 sec in PHP. By default, session closes after browser shutdown. Knowing that the application log contains a bot, most likely it is doing the same thing. (opening and closing the connections)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM