PHP SESSION变量已更改,但视为空
I have a form where the user inputs data and submits. This form does not refresh when the user submits and uses Ajax to relay the information to PHP. Upon submission, the user goes to a new page (in reality it's the same page, but the user views it as another). The user can go back to the previous page to change some information if they want. When the user first submits the page, I am having no problem with it. It's when the user goes back and decides to change again is where I'm having trouble. With my current code, I initiate an empty SESSION["prevUrl"] upon refresh. Afterwards, when the user submits the form, the SESSION["prevUrl"] gets assigned the new POST data. Let's say the user decides to submit again, the if/else statement I have should go to the else statement as the SESSION variable is not empty, but instead it decides to invoke the if block. I have used INSERT statements for both if and else block to see which is being invoked, and at both time the if statement is being invoked. Here's my code:
session_start();
ob_start();
$_SESSION["prevUrl"] = "";
if (isset($_POST["articleType"], $_POST["articleTitle"], $_POST["articleUrl"], $_POST["numberOfPages"], $_POST["typeOfArticle"]))
{
$_SESSION["articleType"] = $_POST["articleType"];
$_SESSION["articleTitle"] = filter_data($_POST["articleTitle"]);
$_SESSION["articleUrl"] = friendlyUrl(filter_data($_POST["articleUrl"]));
if (empty($_SESSION["prevUrl"]))
{
$db->query("INSERT INTO Stories (`url`) VALUES ('hi')");
$_SESSION["prevUrl"] = friendlyUrl(filter_data($_POST["articleUrl"]));
}
else
{
$db->query("INSERT INTO Stories (`url`) VALUES ('bye')");
$db->query("UPDATE Stories SET url = '{$_POST['articleUrl']}' WHERE url = '{$_SESSION['prevUrl']}'");
$_SESSION["prevUrl"] = friendlyUrl(filter_data($_POST["articleUrl"]));
}
$_SESSION["numberOfPages"] = $_POST["numberOfPages"];
$_SESSION["typeOfArticle"] = $_POST["typeOfArticle"];
}
How can I fix it so that the second time around, the else block is invoked? And why is it that the second time around, my SESSION["prevUrl"] is considered empty?
我有一个用户输入数据和提交的表单。 当用户提交并使用Ajax将信息中继到PHP时,此表单不会刷新。 提交后,用户转到新页面(实际上它是同一页面,但用户将其视为另一页面)。 如果需要,用户可以返回上一页以更改某些信息。 当用户首次提交页面时,我对它没有任何问题。 当用户返回并决定再次更改时,我遇到了麻烦。 使用我当前的代码,我在刷新时启动一个空的SESSION [“prevUrl”]。 之后,当用户提交表单时,会为SESSION [“prevUrl”]分配新的POST数据。 假设用户决定再次提交,我有的if / else语句应该转到else语句,因为SESSION变量不为空,而是决定调用if块。 我已经为if和else块使用了INSERT语句来查看正在调用的语句,并且两次都调用了if语句。 这是我的代码: p>
session_start();
ob_start();
$ _SESSION [“prevUrl”] =“”;
if(isset($ _ POST) [“articleType”],$ _POST [“articleTitle”],$ _POST [“articleUrl”],$ _POST [“numberOfPages”],$ _POST [“typeOfArticle”]))
{
$ _SESSION [“articleType” ] = $ _POST [“articleType”];
$ _SESSION [“articleTitle”] = filter_data($ _ POST [“articleTitle”]);
$ _SESSION [“articleUrl”] = friendlyUrl(filter_data($ _ POST [ “articleUrl”]));
if(empty($ _ SESSION [“prevUrl”]))
{
$ db-> query(“INSERT INTO Stories(`url`)VALUES('hi')” );
$ _SESSION [“prevUrl”] = friendlyUrl(filter_data($ _ POST [“articleUrl”]));
}
else
{
$ db-> query(“INSERT INTO Stories” (`url`)VALUES('bye')“);
$ db-> query(”UPDATE Stories SET url ='{$ _POST ['articleUrl']}'WHERE url ='{$ _SESSION ['prevUrl ''}'');
$ _SESSION [“prevUrl”] = friendlyUrl(filter_data($ _ POST [“articleUrl”]));
}
$ _SESSION [“numberOfPages”] = $ _POST [“numberOfPages”];
$ _SESSION [“typeOfArticle”] = $ _POST [“typeOfArticle”];
}
code> pre>
\ n 如何修复它以便第二次调用else块? 为什么第二次,我的SESSION [“prevUrl”]被认为是空的? p>
div>
I would do the following:
- Reset everything there is no
$_POSTvariable, because it means that the page was freshly loaded - Add a flag to session that is set to true on the first submit.
- Look for that flag each time. If it is unset, meaning the form hasn't been submitted since the last reload, empty the
prevUrl
Putting these steps together:
session_start();
ob_start();
//reset all session vars when page is freshly loaded
if(empty($_POST)) session_unset();
//clear prevURL only on first submission after a page load
if(empty($_SESSION['inserted']) $_SESSION["prevUrl"] = "";
...
if (empty($_SESSION["prevUrl"]))
{
$result = $db->query("INSERT INTO Stories (`url`) VALUES ('hi')");
if(!$result) die($db->error);
$_SESSION['inserted'] = true;
$_SESSION["prevUrl"] = friendlyUrl(filter_data($_POST["articleUrl"]));
}
else...
because in the beginning of your code you wrote $_SESSION["prevUrl"] = "";(line 3), so, everytime the page executes the SESSION['prevUrl'] goes empty. To solve this, put it as:
if(!isset($_SESSION['prevUrl'])){
$_SESSION['prevUrl'] = "";
}