Recalling Session Variables throughout site

Hey

I cannot seem to recall my session variables throughout my site and i cannot see where i am going wrong.

I have variables in my flash file which i have posted into my ‘info.php’ all working fine, i declare the post variables in ‘info.php’ with the following:

[B]$num[/B] = $_POST['number'];
	[B]$choice[/B] = $_POST['choice'];
	[B]$colour[/B] = $_POST['colour'];

These variables then get called in a simple form that is echo’d as below:

echo '<div id="info">
    <form name="form" method="post" action="">
    <table width="450" border="0" cellspacing="1" cellpadding="3">
    <tr><td width="65&#37;"><strong>Number</strong></td>
    <td width="65%"><strong>Choice</strong></td>
    <td width="20%"><strong>Colour</strong></td>
    <td width="15%"><strong>Quantity</strong></td></tr>
    <tr><td>[B]'.$num.'[/B]</td><td>[B]'.$choice.'[/B]</td><td><input type="text" name="colour" value="[B]'.$colour.'[/B]"readonly></td>
    <td><input type="text" name="qty1" size="5" value="1"> </td></tr>
    <tr valign="bottom"><td><br>Paying with: <br>
    <input type="radio" name="x" value="CashPayment" checked="checked">Cash<br>
    <input type="radio" name="x" value="CardPayment">Card</td>
    <input type="submit" name="Submit" value="Submit"></td></tr>
    </table>
    </form>';

I then set the post variables to session variables with the following:

$num = $_POST['number'];
	$choice = $_POST['choice'];
	$colour = $_POST['colour'];
        [B]$_SESSION['num'][/B] = $_POST['number'];
	[B]$_SESSION['choice'][/B] = $_POST['choice'];
	[B]$_SESSION['colour'][/B] = $_POST['colour'];

So the form has the session variables stated as in:

echo '<div id="info">
    <form name="form" method="post" action="">
    <table width="450" border="0" cellspacing="1" cellpadding="3">
    <tr><td width="65%"><strong>Number</strong></td>
    <td width="65%"><strong>Choice</strong></td>
    <td width="20%"><strong>Colour</strong></td>
    <td width="15%"><strong>Quantity</strong></td></tr>
    <tr><td>[B]'. $_SESSION['num'].'[/B]</td><td>[B]'. $_SESSION['choice'].'[/B]</td><td><input type="text" name="colour" value="[B]'. $_SESSION['colour'].'[/B]"readonly></td>
    <td><input type="text" name="qty1" size="5" value="1"> </td></tr>
    <tr valign="bottom"><td><br>Paying with: <br>
    <input type="radio" name="x" value="CashPayment" checked="checked">Cash<br>
    <input type="radio" name="x" value="CardPayment">Card</td>
    <input type="submit" name="Submit" value="Submit"></td></tr>
    </table>
    </form>';

I have

<?php
session_start();

At the top of every php page.

Can anyone help me with what i am doing wrong? The variables display when you first go to ‘info.php’ but as soon as you navigate away and then back the fields are empty.

Any ideas?

Many thanks

Jon

Are you overwriting them with empty $_POST values since you’re not reposting the form when you navigate back?

Register globals may overwrite them.
Give session variables different names

Quick responses thanks!

When you say Give session variables different names - i have tried

FROM THIS:

$num = $_POST['number'];
$choice = $_POST['choice'];
$colour = $_POST['colour'];
$_SESSION['num'] = $_POST['number'];
$_SESSION['choice'] = $_POST['choice'];
$_SESSION['colour'] = $_POST['colour'];

TO THIS:

$num = $_POST['number'];
$choice = $_POST['choice'];
$colour = $_POST['colour'];
$_SESSION['number'] = $_POST['number'];
$_SESSION['choicefinal'] = $_POST['choice'];
$_SESSION['colourfinal'] = $_POST['colour'];

Shrapnel_N5 - Is that what you meant? That still gives the same problem.

Dan Grossman - You are right i am not reposting the form when you navigate back to ‘info.php’. Only one of the webpages holds the flash file thats posts the original variables.

If it is overwriting them with empty $_POST values am i able to solve this?

Thanks

You can debug your code to find exact line where session variables got actually rewritten.

Also check if session actually starts and has same session id.
Track session cookie.
Ensure you can see any error message occurs

A quick test could be to do a “print_r” on both $_SESSION and $_POST to see what they contain.


<?php
session_start();

echo "POST array contains: ";
print_r($_POST);

echo "Session contains: ";
print_r($_SESSION);

?>

If its not that easy to read, then wrap it in a html pre tags:


<?php
session_start();

echo "<pre>POST array contains: ";
print_r($_POST);
echo "</pre>";

echo "<pre>Session contains: ";
print_r($_SESSION);
echo "</pre>";
?>

I have httpfox, using the cookies display what should i be looking out for then?

When going to the homepage i get

cookie sent:
PHPSESSID - value = 3b66…etc

Which i am assuming is my session ID that should be constant throughout? - Having tested going through into ‘info.php’ and then navigating back and forth between it the session value does stay the same.

I also get cookies sent called utma/utmz/utmc/utmb - are they relevant?

That PHPSESSID is the only cookie that will be created for the session. The actual session data is stored on the server.

utma/utmz/etc are from your Google Analytics tracker. They stand for Urchin Tracker – Urchin was the company that made that product before Google bought them out.

Great thanks! Sorry if this should all be simple for me! am trying to learn as quickly as possible!

zalucius - i have done what you said and have tested your code onto one of my other pages - ‘about-us.php’, the results are blank on first visit which is correct, then once you go through the flash file and post the variables to ‘info.php’ i can navigate around the site completely and everytime i go back to ‘about-us.php’ the stored session variables are displayed correctly. It is only when i go back to ‘info.php’ again that the session variables then disappear on ‘about-us.php’

So it seems that the session variables are stored correctly but for some reason when i go back to ‘info.php’ where they should still be in the form, they are no longer stored…

Any ideas on this??

$_SESSION['number'] = $_POST['number'];
$_SESSION['choicefinal'] = $_POST['choice'];
$_SESSION['colourfinal'] = $_POST['colour'];

When you revisit info.php without POSTing from the Flash file again, $_POST[‘number’], $POST[‘choice’] and $_POST[‘colour’] have no value. You assign these to the session variables, making them empty as well.

Ah ok, so at the moment where my current sites works like:

Flash file (send posted varables)
V
V
info.php (gets posted variables + creates session variables)

Which overrides the session variables whenever i revisit that page.

If i was to add in extra page so it becomes:

Flash file (send posted varables)
V
V
NEW page which is purely the link between flash and ‘info.php’ (gets posted variables + creates session variables)
V
V
info.php (recalls session variables)

Which in theory should mean that the NEW page is used just once between flash and info so that now when i go to ‘info.php’ i am not declaring the post variables at just, just recalling the session variables meaning they don’t become empty…

Would this work?

you can do a little conditional logic on the session variables.


<?php
session_start();

// test if $_POST['number'] actually exists.
if(isset($_POST['number']))
{
   $_SESSION['number'] = $_POST['number'];
   // and the rest....
}

// then test if the session got the value...
if(!isset($_SESSION['number']))
{
    echo "Session['number'] is empty...";
}
?>

That way youre able to catch problems, first if no data is present in the $_POST array, and then later if the $_SESSION array is empty

Its always a good idea to do some kind of data validation, when working with $_POST, $_GET and $_COOKIE…

zalucius - thank you for that code! big big help