Possible Duplicate:
how can I make visitor counter in php?
I want to insert when page open/close or leave but not onunload .. the below code help me to insert when open the page , but not when close or leave
session_start();
if (!isset($_SESSION["visits"]))
$_SESSION["visits"] = 0;
if ($_SESSION["visits"] > 1){
echo 'visit='.$_SESSION["visits"];
echo "You hit the refresh button!";}
else{
mysql_query(
"INSERT INTO najd_visit( visit_userId, visit_staticId, visit_page,
visit_enterTime)VALUES ('$userId', '$Sid', '$title', '$date') ");
$_SESSION["visits"] = $_SESSION["visits"] + 1;
echo 'visit='.$_SESSION["visits"];
echo "This is my site";
}
You cant run something via PHP when someone "leaves" the page - because the browser nevers sends anything to your server to trigger the script.
You only possible option is to use javascript and run it as they leave - but its going to be temperamental and unreliable at best.