3

我正在使用此方法根据从上一页传递的变量显示不同的内容。

    <?php
    if (isset($_GET['click'])) {
        if($_GET['click'] == 'person'){
          file_put_contents('person.txt', ((int) file_get_contents('person.txt')) + 1);
          include 'resultperson.php';
        } elseif ($_GET['click'] == 'text'){
          file_put_contents('text.txt', ((int) file_get_contents('text.txt')) + 1);
          include 'resulttext.php';
        } elseif ($_GET['click'] == 'online'){
          file_put_contents('online.txt', ((int) file_get_contents('online.txt')) + 1);
          include 'resultonline.php';
        }
    } else {
        include 'resulttext.php';
    } 
    ?>  

问题是,当我点击刷新时,该file_put_contents()功能将再次执行。这只是我用来跟踪用户点击该按钮多少次的一种方式。

如何防止在刷新页面时注入整数的增量?

或者如果有一个更简单的方法来做这一切?

4

6 回答 6

1

使用会话可能是您最好的选择,除非您不介意当用户关闭浏览器并再次访问该页面时它会被触发。

// This will be set to true if the user has the session variable set
$clicked = isset($_SESSION['clicked']);

// Check if get variable 'click' is set, and that $clicked is false. 
// If 'click is set and $clicked if false, the variable $click is set to the 
// value of $_GET['click'] (for example 'person'). Other wise it will be set to false.
$click = (isset($_GET['click']) && !$clicked) ? $_GET['click'] : false;   

// Set the session variable 'clicked' if it isn't set so that the next time 
// the user visits, we'll know
if (!$clicked) {
    $_SESSION['clicked'] = 1;
}

if($click == 'person'){
    file_put_contents('person.txt', ((int) file_get_contents('person.txt')) + 1);
    include 'resultperson.php';
} elseif ($click == 'text'){
    file_put_contents('text.txt', ((int) file_get_contents('text.txt')) + 1);
    include 'resulttext.php';
} elseif ($click == 'online'){
    file_put_contents('online.txt', ((int) file_get_contents('online.txt')) + 1);
    include 'resultonline.php';
} else {
    include 'resulttext.php';
}
于 2012-09-15T15:09:21.760 回答
0

要区分用户何时访问该页面与您之前的页面,而不是点击刷新,请查看$_SERVER["HTTP_REFERER"]变量。该变量将包含用户访问时上一页的 url,但在刷新的情况下,该变量将不存在,因为用户没有来自任何页面(他没有点击链接来访问您的页面)而是发送请求的浏览器

如果您的上一页因此被称为“mypage.php”,请替换您的以下行:

if (isset($_GET['click'])) {

对于这个:

if (isset($_GET['click']) && isset($_SERVER["HTTP_REFERER"]) && strpos($_SERVER["HTTP_REFERER"],'mypage.php')) {

那应该做你想做的事。

更新

上述解决方案将不起作用,因为在浏览器上点击刷新时,页面将保持相同的 Referer 值。

于 2012-09-15T15:08:23.867 回答
0
  1. 您可以使用 ajax 调用来触发该操作。这样,网址就被称为“幕后”,而无需重新加载内容。我推荐这个选项;使用JQuery ajax它可以像编写一个 onClick 函数 function clicked(){$.ajax("yourClickCounter.php");}然后添加<button ... onClick="clicked()">...</button>到 html 一样简单。当然,您必须在单独的 PHP 文件中完成 clickCounting,但它的优点是每次单击按钮时都不会重新加载所有内容。

  2. 您可以使用会话变量,例如$_SESSION['has_clicked']. 这要求您首先开始一个会话,使用session_start()

于 2012-09-15T15:08:54.270 回答
0

如果要跟踪用户来自哪里,可以使用 http 标头的 referer 字段。

如果这不是一个选项,您可以将单击链接指向注册单击的 php 页面,然后对header("Location: x.php");显示内容的页面执行 a。

于 2012-09-15T15:09:30.890 回答
0

这应该可以工作,并且更简单,更容易扩展:

<?php
if (isset($_GET['click']) && isset($_SERVER["HTTP_REFERER"])) {
    $click = $_GET['click'];
    if (in_array($click, array('person', 'text', 'online')) {
      file_put_contents($click . '.txt', ((int) file_get_contents($click . '.txt')) + 1);
      include 'result' . $click . '.php';
    }
} else {
    include 'resulttext.php';
} 
?>
于 2012-09-15T15:14:27.343 回答
0

使用您的实际代码,您无法阻止页面刷新时整数的增量。

问题是:重要吗?您希望您的追踪器有多重要和准确?

有一种方法可以将增量附加到按钮单击上,即使用 AJAX。您可以将处理程序附加到按钮单击事件,从而对 PHP 脚本进行适当的 AJAX 调用。

这并不简单,但很简单。

此解决方案和您的实际解决方案都无法阻止恶意用户发出 HTTP 请求来调用您的 PHP 脚本,从而随意增加您的计数器......

于 2012-09-15T15:10:21.933 回答