1
以下脚本更新浏览量(如果其来自独特访客)。该页面从数据库检索博客文章并在屏幕上打印。当第一次访问博客文章时,脚本应该将其网页浏览字段更新为1.但是,脚本正在更新每个页面刷新时的浏览量,而不是只记录唯一视图。PHP独特的浏览量计数器无法正常工作
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'并看看里面有什么? –