Question:
Why PHP $_SESSION variable becomes unset after page reload?
Babar Shahzad Chaudary
2010-09-29 02:40:13 UTC
I am initializing a session variable on my web page like $_SESSION['msg'] = $_POST['test'];. Then page is reloaded. & when page reloads, the $_SESSION['msg'] is empty. Can anyone help me to recover this problem.
session_start();
$_SESSION['msg'] = $_POST['test']; // $_POST['test'] has value
header('Location: ./test.php'); //test.php also contains session_start() statement
if(isset(
$_SESSION['msg']))
{ print 'set'; } else
{ print 'not-set'; } //prints not-set
?>
Three answers:
rbjolly
2010-09-29 07:05:05 UTC
What I think is happening is that on first call, $_POST['test'] is set and so $_SESSION['msg'] gets set. Then you redirect to test.php but do not propagate the post data so now $_POST['test'] is no longer set. So when you redirect back from test.php and assign $_POST['test'] to $_SESSION['msg'], $_SESSION['msg'] becomes undefined because $_POST['test'] is now undefined.



What I think you need to do to fix this problem is replace:



$_SESSION['msg'] = $_POST['test'];



With



if (isset($_POST['test'])) $_SESSION['msg'] = $_POST['test'];
bae
2016-11-11 01:49:45 UTC
Php Unset Session Variable
anonymous
2016-11-02 09:01:51 UTC
you will possibly desire to placed session_start(); in any web site you will possibly desire to get right of entry to the consultation documents here you redirect to connect.very own domicile page so in case you place session_start(); there too you will possibly desire to have the potential to get right of entry to to tweetmsg variable


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...