dqqn32019 2014-12-07 13:06
浏览 22
已采纳

Mysql Pageviews仅在每个会话中更新

just got help with making my code for pageviews work... and now i would like the pageviews only count 1 for each session / ip.

In my users (news.users) table i have these cells available for use: (tried to set up something earlier that didnt work out so these are not in use)

ip_reg, ip_visit, dtreg, dtvisit, visits, pass

The code i have that is now working looks like this :

//Adds one to the counter 

  mysql_query("UPDATE news SET posts = posts + 1, published=published, last_edit=last_edit WHERE id=$id");

//Retreives the current count 
$count = mysql_fetch_row(mysql_query("SELECT posts FROM news WHERE id=$id"));
//Displays the count on your site print 


echo "<label>Viewed: ";
echo $count[0];
echo " times</label>";

?> 

Can i use some of the unused cells in users to stop a user from reloading the page to get higher views on a page? And how do i do it :P

  • 写回答

1条回答 默认 最新

  • dongsui8162 2014-12-07 13:28
    关注

    Like I suggested I would put the read news articles in the user's session. Try this:

    if (!isset($_SESSION['read_news']) || !is_array($_SESSION['read_news'])) {
        $_SESSION['read_news'] = array();
    }
    
    if (!in_array($id, $_SESSION['read_news'])) {
        $_SESSION['read_news'][] = $id;
        mysql_query("UPDATE news SET posts = posts + 1 WHERE id=$id");
    }
    

    If this doesn't work, you don't have sessions enabled and you should put session_start(); somewhere in the top of your project. Make sure that line is called in each request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制