gpt4 book ai didi

php - 根据变量包含不同的内容

转载 作者:搜寻专家 更新时间:2023-10-31 21:39:54 25 4
gpt4 key购买 nike

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

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

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

或者是否有更简单的方法来完成这一切?

最佳答案

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

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

关于php - 根据变量包含不同的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12438428/

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