为什么此代码不能正确增加计数器? [英] Why does this code not increment a counter properly?
问题描述
在我下面的代码中,为什么不是每次都将整数加一?例如,假设我提交表单时有 1 OF 5
.提交后,应该是2 OF 5
,而是显示5 OF 5
.即使我将最大值从 5 更改为 3,也会发生这种情况;它从 1 OF 3
开始并立即跳转到 3 OF 3
.这是显示 .. OF ..
In my code below, why is it not incrementing the integers by one each time? For example, say I have 1 OF 5
when I submit the form. After submission, it should be 2 OF 5
, but instead, it displays 5 OF 5
. This occurs even if I change the maximum from 5 to 3; it starts at 1 OF 3
and jumps immediately to 3 OF 3
. This is the code to display the .. OF ..
<h1><?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?></h1>
下面是每次将值加一的代码:
Below is the code which should increment the value by one each time:
if(isset($_POST['sessionNum'])){
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['sessionNum'];
$_SESSION['sessionNum'] = $_POST['sessionNum'];
$_SESSION['sessionCount'] = 1;
}
else if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
$_SESSION['sessionCount']++;
}
$sessionMinus = $_SESSION['sessionCount'];
推荐答案
代码看起来不错.但是,该页面可能在您不知情的情况下在后台被请求.例如,损坏的图像路径等可能会落在此文件上,因此会增加您的计数器.为了确保不是这种情况,请添加一个新的会话数组 var 来记录 $_SERVER['REQUEST_URI']
或将其记录到文本文件或其他内容中.
The code looks fine. However it is possible that the page is being requested in the background without your knowing. For instance a broken image path etc could be landing on this file hence incrementing your counter. To make sure thats not the case, add a new session array var which records the $_SERVER['REQUEST_URI']
or log it to a text file or something.
这篇关于为什么此代码不能正确增加计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!