页面刷新后,PHP会话变量为空 [英] PHP session variables blank after page refresh

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

问题描述

我早些时候写了一篇关于这个的文章,得到了很好的答复,纠正了我的一个错误,我认为这个错误解决了这个问题.不幸的是,事实并非如此,我的旧帖子太乱了,我将重新发布该问题,但是从更多的角度来看,我已经为此苦苦挣扎了一段时间.

I wrote a post about this earlier and got good responses that corrected a mistake I had, which I thought fixed the problem. Unfortunately it didn't, and my old post is messy enough that I'm going to repost the problem, but with the added perspective I have gained struggling with this for another good while.

代码(简体):

<?php
session_start();

if (!$_SESSION["companyid"]) {

header("location: http://www.somepage.com");

}

mysql_connect("localhost", "name", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$companyid = $_SESSION["companyid"];

$_SESSION["youtubeurl"] = mysql_real_escape_string($_POST["youtubeurl"]);
$_SESSION["logourl"] = mysql_real_escape_string($_POST["logourl"]);

$_SESSION["plan1head"] = mysql_real_escape_string($_POST["plan1head"]);
$_SESSION["plan1description"] = mysql_real_escape_string($_POST["plan1description"]);
$_SESSION["plan1headline1"] = mysql_real_escape_string($_POST["plan1headline1"]);
$_SESSION["plan1price1"] = mysql_real_escape_string($_POST["plan1price1"]);
$_SESSION["plan1headline2"] = mysql_real_escape_string($_POST["plan1headline2"]);
$_SESSION["plan1price2"] = mysql_real_escape_string($_POST["plan1price2"]);
$_SESSION["plan1price1type"] = mysql_real_escape_string($_POST["plan1price1type"]);
$_SESSION["plan1price2type"] = mysql_real_escape_string($_POST["plan1price2type"]);

if(isset($_POST["submitpreview"])) {

$companyid = $_SESSION["companyid"];

$youtubeurl = $_SESSION["youtubeurl"];
$logourl = $_SESSION["logourl"];
$plan1head = $_SESSION["plan1head"];
$plan1description = $_SESSION["plan1description"];
$plan1headline1 = $_SESSION["plan1headline1"];
$plan1price1 = $_SESSION["plan1price1"];
$plan1headline2 = $_SESSION["plan1headline2"];
$plan1price2 = $_SESSION["plan1price2"];
$plan1price1type = $_SESSION["plan1price1type"];
$plan1price2type = $_SESSION["plan1price2type"];

}

?>

现在,涉及三个页面/实例:

Now, there are three pages/instances involved with this:

  1. 具有将变量传递到预览页面的表单的页面
  2. 预览页面
  3. 提交的预览页面(意味着页面已刷新)

一个人有一个自己的页面,其中充满了各种文本字段和下拉菜单(第1页).他们可以更新此页面,并在填写表单并提交时将这些变量传递到预览页面.如果他们喜欢预览页,则对其进行确认(提交执行"submitpreview"的表单).

A person has their own page filled with various text fields and drop-down menus (page 1). They can update this page, and when they fill out the form to do that and submit it, those variables are passed onto the preview page. If they like the preview page, that confirm it (submitting a form that executes "submitpreview").

这是我的问题:表单填写的所有会话变量(除公司ID外,所有其他变量都在首次登录时存储在会话中)立即被转储到会话变量中.我已经在预览页面的底部回显了这些会话变量(只是为了确认它们此时不为空),当我进入预览页面时,它们的内容会被适当地回显.但是,当用户确认更改并设置了commitpreview时,这些会话变量突然为空.也就是说,所有由表单变量填充的会话变量都是空的.在刷新页面之前呼应的会话变量已经消失,除了companyid变量.由于companyid会话变量仍然很好,因此我知道(1)服务器上的会话必须正常工作,并且(2)问题出在将表单变量转储到会话中或检索这些变量的代码中.关于为什么会发生这种现象的任何理论?

Here is my problem: All the session variables that are being filled by the form (everything but company id, which is stored in a session when they first log in) are immediately dumped into a session variable. I have echoed out those session variables at the bottom of the preview page (just to confirm they are not empty at this point in time), and their contents are echoed out appropriately when I get to the preview page. However, when the user confirms the changes and submitpreview is set, suddenly those session variables are empty. That is, all the session variables that were filled by the form variables are empty. The session variables that echoed out just fine before the page was refreshed are gone except for the companyid variable. Since the companyid session variable is still just fine, so I know that (1) sessions on my server must be working right and (2) the problem lies with the code that is either dumping the form variables into the sessions or retrieving those variables. Any theories as to why this may be occurring?

这让我非常沮丧.感谢您在此问题上的耐心配合,并感谢您提供的任何帮助.

This has me pretty frustrated. I appreciate your patience with me on this issue and appreciate any help that is given.

推荐答案

想通了,以为我会分享.

Figured it out, thought I would share.

在这种情况下,当会话变量等于刷新页面上的发布变量时,请务必小心.当我的预览页面刷新时,发布变量(因为它们不再存在)变为空,这覆盖了我之前保存的会话变量的信息,从而使这些会话变量也变空了.我必须相应地调整代码以解决该问题.

In instances such as this when your have session variables equal to post variables on a page that refreshes, you to have be careful. When my preview page would refresh, the post variables (since they no longer existed) became empty, which overwrote the information for the session variables I had previously saved and in turn made those session variables empty as well. I had to adjust my code accordingly to fix that problem.

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

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