Jump to content

Session Variable


Bottyz

Recommended Posts

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!

 

Link to comment
https://forums.phpfreaks.com/topic/190907-session-variable/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/190907-session-variable/#findComment-1006740
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.