根据变量包括不同的内容


Include different content based on variables

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

    <?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()函数将再次执行。这只是我用来跟踪用户点击该按钮的次数的一种方式。

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

或者,如果有一种更简单的方法来完成这一切?

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

// 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';
}

如果您想跟踪用户的来源,可以使用http头的referer字段。

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

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

问题是:这有关系吗?你希望你的追踪器有多重要和准确?

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

这并不简单,但很简单。

这个解决方案和你的实际解决方案都无法阻止恶意用户发出HTTP请求来调用你的PHP脚本,随意增加你的计数器。。。

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

<?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';
} 
?>
  1. 您可以使用ajax调用来触发该操作。通过这种方式,url被称为"幕后",而无需重新加载内容。我推荐这个选项;使用JQuery-ajax,只需编写像function clicked(){$.ajax("yourClickCounter.php");}这样的onClick函数,然后将<button ... onClick="clicked()">...</button>添加到html中即可。当然,您必须在一个单独的PHP文件中完成clickCounting,但它的优点是不会在每次单击按钮时重新加载所有内容。

  2. 您可以使用像$_SESSION['has_clicked']这样的会话变量。这要求您首先使用session_start() 启动会话

要区分用户何时访问上一页的页面,而不是点击刷新,请查看$_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值。