I want to enter only one detail in table per user visit. Irrespective of which page he lands on, his visit counts. So i inserted this in header.php //Header file included in every php file.
//$retuser is not defined in veryfirst visit
if(!isset($retuser)){
$ip=getRealIpAddr();
if(isset($_SESSION['user_id'])) {
$uid=$_SESSION['user_id'];
} else {$uid=0;
}
$query="insert into visitors (vistime,visip,visiden) values(now(),'{$ip}','{$uid}')";
$result = mysql_query($query,$connection);
if (!$result) {
echo "DB Error, could not insert comments
";
echo 'MySQL Error: ' . mysql_error();
exit;
}
$retuser=1;
}
Now this is inserting user entry on every page reloading. How to solve this?
UPDATE :
I want $retuser to work or live until user closes his tab. Upon closing of tab $retuser must be destroyed!
A few things to keep in mind when you're trying to track visitors, luckily you're using a user_id so you don't have to worry about a lot of the issues that arise with tracking casual browsers (non-authenticated):
You can pull a query to see if the user has been on the site within a certain amount of time. The problem however is if a user is legitimate, leaves their browser open because they went to lunch or to the restroom and comes back 10 minutes later, that SESSION
is still active. They've not gone anywhere, in fact if they hit another page, this is the type of person you want to keep. If you flush their session because of the delay you're shorting yourself of the potential track.
You can use Javascript components to track when they last moved their mouse or moved away from your page with focus, but it may make your site appear slower if the listeners are greedy. Additionally someone can disable Javascript or block your script and still legitimately browse your site.