php会话变量没有更新 [英] php session variables not updating

查看:50
本文介绍了php会话变量没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 php 会话变量,我在一个页面中更新它并在不同的页面中访问它,当变量达到阈值时,我将其重置为零并根据上传的文件数量增加它.

I have a php session variable and I update it in a page and access it in different page, when the variable comes to a threshold I reset it to zero and increment it according to the number of files uploaded.

问题是当我重新设置并重新加载页面时它达到阈值后,我访问会话变量的文件没有更新并且虽然文件已上传但显示为零.

The problem is after it reaches to the threshold when I reset and reload the page again the file that I access the session variable not updating and shows zero though the files are uploaded.

但实际上会话变量在另一个页面中增加.只显示两次重载后的实际值(增加的值).

But actually the session variable is incrementing in the other page. It shows the real value(the value incremented) after only reloading twice.

我想知道为什么会这样.

I want to know why it happens.

推荐答案

首先,显示您的问题的代码很重要,但对于这个特定的问题,问题在于您使用的是 session variables 就在您更新它们的文件中.会话在页面中的任何内容之前加载,因此,当您更新它们时,它们已经加载,尽管它们已更改,但它们的值在当前页面中仍然相同.为防止这种情况发生,您需要在更新会话后转到另一个页面,或者如果需要保持相同的地址,则在会话更改语句之后放置重定向,如下所示:

First of all, It is critical to show your codes for your question, but for this particular one, the problem is that you are using session variables right in the file that you updated them. sessions load before anything in the page so, when you update them they are already loaded and although they have changed, their value is still the same in current page. To prevent that, you need to either go to another page after updating the session or put a redirect after your session change statement if it is necessary to stay in same address, like this:

<?php
if(threshold){
$_SESSION['your_session']='your new value';
header('LOCATION sample.php');
}
?>

这篇关于php会话变量没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆