【发布时间】:2017-05-22 03:44:27
【问题描述】:
以下脚本会更新来自唯一身份访问者的综合浏览量。该页面从数据库中检索博客文章并在屏幕上打印。当第一次访问博客文章时,脚本应该将其页面浏览量字段更新 1。但脚本会在每次页面刷新时更新页面浏览量,而不是只记录唯一的浏览量。
if($_SESSION[$isPostID] != $isPostID)
{
try
{
$updatePageViews = $db2->prepare("UPDATE articles SET pageviews = pageviews+1 WHERE id = :id");
$updatePageViews->execute(array(':id' => $isPostID));
if($updatePageViews->rowCount() != 1)
{
@createLog("Unable to update pageviews.","Unable to update pageviews!!! Title = [".$istitle."].");
}
else{ $_SESSION[$isPostID] = $isPostID;}
}
catch(PDOException $updatePageViewsERR)
{
$subject = "Pageviews Updation--Update data into database. [PAGE= ".$istitle."]. Error Code: #15";
$text = $updatePageViewsERR->getMessage();
@createLog($subject,$text);
}
}
$isPostID 是分配给数据库表中每个博客文章的唯一 ID。 注意:会话已经在脚本中启动。
【问题讨论】:
-
首先,您确定会话机制有效吗? session_start() 是否到位/自动化?你试过把
$_SESSION打印出来看看里面有什么吗?
标签: php mysql web-analytics pageviews