简体   繁体   English

我不知道为什么我的 PHP Session 变量在页面重定向上被覆盖

[英]I can't figure out why my PHP Session variables are being overwritten on page redirect

I have a page using session variables that are created from user form input via the POST method.我有一个使用 session 变量的页面,这些变量是通过 POST 方法从用户表单输入创建的。 The session variables are being created properly using this code:正在使用以下代码正确创建 session 变量:

(I am collecting a zip code via an input on a form) (我正在通过表单上的输入收集 zip 代码)

<form name="zipinput" id="zipform" method="post" action="" target="_blank" onsubmit="return valZip();"/>     
 <input id="zip" name="zip" class="zipfield" type="text" value="" />
 </form> 

Here is my PHP code:这是我的 PHP 代码:

$_SESSION['zip'] = $_POST['zip'];
if(isset($_SESSION['zip'])) {
$zipcode = $_SESSION['zip'];
}

This works fine.这工作正常。 But when I go from page1.php to page2.php, the variables are erased.但是当我 go 从 page1.php 到 page2.php 时,变量被删除。 I thought the whole point of session variables was to be able to use them from page to page without _GET or _POST in the URL.我认为 session 变量的全部意义在于能够在 URL 中在没有 _GET 或 _POST 的情况下逐页使用它们。 I am also including: session_start();我还包括: session_start(); at the top of my php pages.在我的 php 页面的顶部。

Any ideas would be greatly appreciated.任何想法将不胜感激。 Thanks.谢谢。

That's because you're assigning a value to the session whether or not a zip value was actually provided.这是因为无论是否实际提供了 zip 值,您都在为 session 分配一个值。 If not zip parameter is presesnt, you just write in a blank, which erases the previous zip.如果不存在 zip 参数,则只需在空白处写入,这将擦除之前的 zip。 You'd want to do:你想做:

if (isset($_POST['zip'])) {
   $_SESSION['zip'] = $_POST['zip'];
}
$zip = $_SESSION['zip'];
if(isset($_POST['zip'])
$_SESSION['zip'] = $_POST['zip'];
if(isset($_SESSION['zip'])) {
$zipcode = $_SESSION['zip'];
}

instead of just having session_start() at the top of the page, try having而不是只在页面顶部有 session_start() ,尝试有

if(!isset($_SESSION)):
    session_start();
endif;

You must start a session on all pages.您必须在所有页面上启动 session。 This is a good candidate for being put in an init file to arbitrarily be loaded in all pages before any other page content.. like in the header.这是一个很好的候选者,可以在任何其他页面内容之前被放入 init 文件中任意加载到所有页面中。就像在 header 中一样。

If you don't, the session you saved will be destroyed upon page redirect.如果不这样做,您保存的 session 将在页面重定向时被销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP包含。 我不知道为什么我不能运行我的功能 - PHP Include. I can't figure out why I can't run my functions 会话变量被覆盖 - Session variables being overwritten 我的简单 PHP 登录脚本没有按预期工作,我不知道为什么 - my simple PHP login script is not working as intended and i can't figure out why 无法弄清楚为什么我的PHP代码出现服务器错误 - Can't figure out why I'm getting Server errors from my PHP code 我的关联数组是多维的,我不知道为什么。 的PHP - My Associative array is multi dimensional, and i can't figure out why. PHP 我无法弄清楚为什么其中一个PHP脚本无法正常工作 - I can't figure out why one of these PHP scripts is not working 为什么我不能从我的AJAX调用的PHP脚本中访问会话变量? - Why can't I access session variables from my AJAX-called PHP script? 在我的实时应用程序上出现行为,这很像会话冲突。 不知道为什么 - Getting behavior on my live app that seems a lot like session collisions. Can't figure out why 我不知道php语法 - I can't figure out php syntax 一旦我从登录名重定向到管理页面,PHP会话变量就不会转移到我的管理页面 - PHP session variables not carrying over to my admin page once I redirect from login to admin page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM