可能重复:
如何在 php 中制作访客计数器?
我想在页面打开/关闭或离开时插入,但不是 onunload .. 下面的代码帮助我在打开页面时插入,但在关闭或离开时不插入
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";
}