Bottyz Posted February 4, 2010 Share Posted February 4, 2010 Hey all, I seem to be here a lot lately! Just have a question: If you set a form to use a seperate php file as its action, which you then set a session variable in that seperate php file and get it to redirect the header back to the first php file, can you echo the session variable without it unsetting itself? Because i can't get it to work? one.php if(isset($_SESSION['Msg'])) { //print my message if it exists echo $_SESSION['Msg']; //now clear it again $_SESSION['Msg'] = NULL; //add a few breaks echo "<br><br>"; } further down in one.php i have the following form with the action to go to two.php <form action="two.php" method="POST"> and two.php sets the session variable and returns to one.php like so: if($goahead == "true") { $_SESSION['Msg'] = "Successfully activated " . $counter . " of " . $total . " users."; } $MM_success = "one.php"; header("Location: " . $MM_success); exit; I've echoed out the results of two.php and all works as it should (including the session variable), but once it redirects back to one.php the session variable doesn't echo anything or seem to exist? Can it be done? Cheers all! Quote Link to comment https://forums.phpfreaks.com/topic/190907-session-variable/ Share on other sites More sharing options...
oni-kun Posted February 4, 2010 Share Posted February 4, 2010 You must place session_start(); at the beginning of each page which requires sessions to run. Quote Link to comment https://forums.phpfreaks.com/topic/190907-session-variable/#findComment-1006736 Share on other sites More sharing options...
Bottyz Posted February 4, 2010 Author Share Posted February 4, 2010 Ignore what i just said, i found the problem by looking at someone else post! it was due to a wrongly set session_start() statement: if (isset($_SESSION)) { session_start(); } obviously i missed the ! before isset! D'oh! Quote Link to comment https://forums.phpfreaks.com/topic/190907-session-variable/#findComment-1006738 Share on other sites More sharing options...
oni-kun Posted February 4, 2010 Share Posted February 4, 2010 Ignore what i just said, i found the problem by looking at someone else post! it was due to a wrongly set session_start() statement: if (isset($_SESSION)) { session_start(); } obviously i missed the ! before isset! D'oh! That code is incorrect, syntactically, session_start() checks and reinitiates the session, and is required to be called at each file. Quote Link to comment https://forums.phpfreaks.com/topic/190907-session-variable/#findComment-1006740 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.