gpt4 book ai didi

php - 如何不通过刷新增加页面/帖子查看次数?

转载 作者:行者123 更新时间:2023-12-04 16:48:24 26 4
gpt4 key购买 nike

我在网上搜索了一种通过刷新页面来禁用帖子/页面 View “黑客攻击”的方法,但我没有找到任何方法。

如何不通过刷新增加页面/帖子浏览量?

或不同角度的同一个问题:

如何只注册独立访问?

我目前的想法是 IPnonce 但恐怕我需要一些帮助才能开始。


当前代码非常简单:

    /* Allow devs to override the meta key used. By default, this is 'Views'. */
$meta_key = 'estate_property_views_count';

/* Get the number of views the post currently has. */
$old_views = get_post_meta( $post_id, $meta_key, true );

/* Add +1 to the number of current views. */
$new_views = absint( $old_views ) + 1;

/* Update the view count with the new view count. */
update_post_meta( $post_id, $meta_key, $new_views, $old_views );

最佳答案

方法 1 - 使用 Cookie:

您可以使用 cookie 代替 IP。您需要首先检查 cookie 是否存在,如果不存在则更新并设置 cookie,否则不更新和设置 cookie。

像这样的东西会起作用:

if(!isset($_COOKIE['not_unique'])) {
setcookie('not_unique', '1', time() + (86400 * 30)); //30 days
$old_views = get_post_meta( $post_id, $meta_key, true );
$new_views = absint( $old_views ) + 1;
update_post_meta( $post_id, $meta_key, $new_views, $old_views );
}

方法 2 - 使用 session :

在脚本的第一行添加 session_start(); 然后像这样使用它:

if(!isset($_SESSION['not_unique'])) {
$_SESSION['not_unique'] = 1;
$old_views = get_post_meta( $post_id, $meta_key, true );
$new_views = absint( $old_views ) + 1;
update_post_meta( $post_id, $meta_key, $new_views, $old_views );
}

关于php - 如何不通过刷新增加页面/帖子查看次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33820035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com