PHP会话变量未保存 [英] PHP Session variable is not saving
问题描述
我已经仔细研究了有关会话变量未保存的所有问题,并且看不到我的问题,所以我要问它。
I've looked through all the problems about the session variable not saving and don't see my problem so I'm going to ask it.
我有提交后的表单会在我的数据库中搜索该名称。初始形式在第1页上。
在第2页上,我从第1页上获取了变量,并保存为这样
I have a form that once submitted it searches my database for that name. The initial form is on page 1. On page 2 I take the the variable from page 1 and save it like this
$searchTerm = $_POST['find'];
它用作数据库的搜索,并且运行良好。在此之下我有我的sql语句,然后将其放置在
which is used as the search for the database and it works perfectly. Under that I have my sql statement then I have placed this
//initialize the session
if (!isset($_SESSION)) {
session_start();
}
$_SESSION['searchTerm'] = $searchTerm;
然后我对第2页的$ _SESSION ['searchTerm']进行了测试,以确保保存正确,它确实可以。当我尝试转到第3页,即第2页中表单的确认页时,出现了我的问题。我在第3页的顶部有session start子句,甚至还插入了
I have then tested the $_SESSION['searchTerm'] on page 2 to make sure that it is saving properly, and it does. My issue comes in when I try to go to a 3rd page that is a confirm page for the form from page 2. I have the session start clause at the top of the 3rd page and I even inserted
ini_set('display_errors',1);
error_reporting(E_ALL);
检查错误,未显示任何错误。因此,我的下一步是测试会话本身,以查看是否正在刷新会话ID。我在此网站www.webassist.com/forums/posts.php?id=5735上找到了一个脚本,以测试服务器是否可能引起此问题。该脚本工作正常,没有问题,表明服务器不是问题。但是,当我将第二页和第三页上传到服务器并放入
to check for errors, no errors were displayed. So my next step was to test the session itself to see if it was refreshing the session id. I found a script on this site www.webassist.com/forums/posts.php?id=5735 to test whether the server could possibly be causing the issue. The script works fine no problems, showing that the server is not the problem. However when I upload my 2nd and 3rd page to the server and put in
<?php echo session_id(); ?>
第2页和第3页的数字完全不同,因此使我的变量为null。我做了进一步的研究,认为可能是因为存在session_destroy或session_unset,但是我没有在任何一个页面上放置其中一个。当我在本地计算机上尝试此操作时,我获得了相同的会话ID,但仍然设置的会话变量为空。
the numbers from page 2 and page 3 are completely different therefore making my variable null. I did further research and thought it might be because there was a session_destroy or session_unset but I didn't put one of these on either page. When I tried this on my local machine I got the same session id but still the session variable I set was blank.
有人对这种方式或原因有其他想法吗?
Does anyone have any other ideas on how or why this would happen?
** * ** * **** 编辑 ** * ** * ** * ** * ** * ** * ** *
**********edit *********************
第1页具有以下格式
<form name="search" method="post" action="page2.php">
<div>
<!-- Search-->
<label>Search by Last Name:</label>
<div>
<table width="100%">
<tr>
<td><input type="text" id="" name="find" placeholder=""></td>
<td><button id="submit" type="submit"><span>Search</span></button></td>
</tr>
</table>
</div>
</div>
</form>
第2页
$searchTerm = $_POST['find'];
if(!empty($searchTerm ) && ctype_alpha($searchTerm ))
{
$whereclause = "WHERE mytable.lastName = '".$searchTerm ."'";
}
else {
$whereclause = "";
}
// sql query is here this one searches it is long and just used the $whereclause above.
// second sql query that is only submitted if button from the form below is hit is here.
$insertGoTo = "confirmPage3.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'] ;
}
header(sprintf("Location: %s", $insertGoTo));
//initialize the session
if (session_id() == '') {
session_start();
}
$_SESSION['searchTerm'] = $searchTerm;
// then the form is listed below that with the rest of the html
第3页
<?php if(session_id() == '') session_start(); ?>
if(!empty($_SESSION['searchTerm']) && ctype_alpha($_SESSION['searchTerm']))
{
$whereclause = "WHERE myTable.lastName = '".$_SESSION['searchTerm'] ."'";
}
else {
$whereclause = "";
}
// More sql statement here for this one it is only selecting not updating or changing anything.
// Table below this that is used to hold the items retrieved from the database.
** * * * * ** * ** 编辑2和我的修复程序 * ** * ** * ** *
***********edit 2 and my fix**********
回来,对于你们中的一些人,龚亚伦建议我从空白页开始,然后从那里开始。我这样做了,终于诊断出了问题。我从来没有想过应该有的东西。我不喜欢使用Dreamweaver,现在我记得为什么。最初创建页面2时,我使用Dreamweavers插入代码插入sql语句进行更新。
so after some back and for with a few of you, Aaron Gong suggested I start with a blank page and go from there. I did this and have finally diagnosed the issue. It was something that I hadn't thought of that I very well should have. I don't like using dreamweaver and now I remember why. When I originally created the page 2 I used dreamweavers insert code to insert my sql statement to do the updating. Well it placed in the code these lines of code.
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
如果您试图将用户发送给其他人,这将完全困扰您页面以确认他们的输入。我将其注释掉,然后将$ editFormAction设置到我的页面,并在标头字符串和中提琴中使用了它,立即修复了我的问题。我没有意识到的是,当刷新页面时,它清空了我的$ _POST变量,并最终通过亚伦建议的空白页面发现了该错误。
This will completely mess with you if you are trying to send the user to another page to confirm their entries. I commented it out and set the $editFormAction to my page and used that in the header string and viola that immediately fixed my issue. What I hadn't realized is when it was refreshing the page that it was emptying my $_POST variable and found that error finally through the empty pages suggest by Aaron.
希望这对其他人有帮助,我将永远不会再信任Dreamweaver的代码。我学会了手工编码,应该会更好。但是我很着急,以为它不会损害它。
Hope this helps someone else and I will never trust the code from dreamweaver again. I learned to code by hand and should know better but I was in a hurry and thought oh it won't harm it.
再次感谢您的所有建议。
Thanks again to all your suggestions.
推荐答案
不确定您的第3页是什么样,但是我的页面是:
Not sure what your 3rd page looks like but mine would be:
session_start(); // Use session variable on this page. This function must put on the top of page.
if( isset($_SESSION['i_am_in']) ) echo "logged in";
我的页面2:
if ($login_ok) {
session_start(); // Create session & settings
$_SESSION['abc123'] = 'whatever';
header('Location: page3.php');
}
这篇关于PHP会话变量未保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!