Hello, session_start(); $total_rows = mysql_num_rows(mysql_query("SELECT * FROM hits WHERE hit_session ='".session_id()."'")); if($total_rows == 0) { mysql_query("INSERT INTO hits SET ip='".$_SERVER[REMOTE_ADDR]'']."', hit_session='".session_id()."'"); } the above code is in hits.php and it is included in all the pages. The hits are getting recorded with different session_id's but very frequenty, even if the user does not close the browser. I think even if the user navigates to another page its getting recorded in the database. Is there anything wrong in the above script? Thanks
You need to call session_id() before session_start(). Try something like this: $total_rows = mysql_num_rows(mysql_query("SELECT * FROM hits WHERE hit_session ='".session_id()."'")); if($total_rows == 0) { mysql_query("INSERT INTO hits SET ip='".$_SERVER[REMOTE_ADDR]'']."', hit_session='".session_id()."'"); } else session_start();
For the first time session_id will be empty. So, the $total_rows will be 0 then it is inserting blank value for hit_session.
Haven't thought about that... well, this makes things a lot easier. if (session_id()==0) { mysql_query("INSERT INTO hits SET ip='".$_SERVER[REMOTE_ADDR]'']."', hit_session='this isnt needed anymore'"); session_start(); } On the first hit, the visitor will have no session, so we record the IP and start a session. On any subsequent hits, a session will already exist, so the script won't go through the "if" anymore.