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
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!

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.