I'm having a hard time with sessions. I have a long form on 3 different pages and then a preview page of everything entered. I created the code to store the session variables on each page but on the preview page it comes up empty.

I am using session_start(); on every page. I have a place where my sessions are stored on my server. Any idea what the problem could be? When I store the information in a session on the first page, will it carry to the 4th page?

Any help will be appreciated!
~Amy

i use sessions all the time and never had a problem. yes, it will carry to the 4th page.

can you post the code, so maybe I can see if you did something wrong?

This is the code on the 1st page:

<?php
session_start();

include ("mydatabaseinfo.inc");

mysql_connect ($host, $user, $password, $database)
or die ("No Connection");

//Form validation:
if((!empty($_POST['street']))
&& (strlen($_POST['street']) < 50))
{
$street=$_POST['street'];
$street=htmlspecialchars($street);
}
else
{
$errors[]= 'You forgot to enter a valid entry=Street!';
}

if (strlen($_POST['unit']) <15)
{
$unit=$_POST['unit'];
$unit=htmlspecialchars($unit);
}
else
{
$errors[]= 'You forgot to enter a valid Unit!';
}

if((!empty($_POST['city']))
&& (strlen($_POST['city']) < 50))
{
$city=$_POST['city'];
$city=htmlspecialchars($city);
}
else
{
$errors[]= 'You forgot to enter a valid entry=City!';
}

if((!empty($_POST['combo0']))
&& (strlen($_POST['combo0']) < 30))
{
$state=$_POST['combo0'];
$state=htmlspecialchars($state);
}
else
{
$errors[]= 'You forgot to enter your State!';
}

if((!empty($_POST['zip']))
&& (preg_match('/(^[0-9]+$)/',$_POST['zip']))
&& (strlen($_POST['zip']) < 10))
{
$zip=$_POST['zip'];
$zip=htmlspecialchars($zip);
}
else
{
$errors[]= 'You forgot to enter a valid Zip Code!';
}

if (empty($errors))
{
header
("Location:http://www.thisnextpage.php");
exit();
}

if (!empty($errors) && is_array($errors))
{
echo '<h1>Error!</h1>
The following error(s) occured:<br/ >';
foreach ($errors as $msg)
{
echo " - $msg<br />\n";
}
}

session_register("street");
session_register("unit");
session_register("city");
session_register("county");
session_register("state");
session_register("zip");

?>

And then this is the code on the 4th page.

<?php
session_start();
include ("mydatabaseinfo.inc");

mysql_connect ($host, $user, $password, $database)
or die ("No Connection");

echo "<html>
<FONT style="FONT-SIZE:14pt" face="Arial Bold">
<center>
{$_SESSION['street']}, {$_SESSION['unit']}<br>\n";
echo "{$_SESSION['city']}, {$_SESSION['state']} 

{$_SESSION['zip']}\n
</center>
</font>
</html>";
?>

My server has php 4 which really sucks, but I'm dealing with it :)

Hope we can get down to the problem :)
~Amy

i recommend using the $_SESSION array to register session vars. anyway, you are declaring the variables after the redirect occurs, so they are not even being set at all.

make sure you add the info to the session before the redirect occurs. i usually do it after my script finds the entire form valid.

i usually do multiple page forms on one page and use a switch statement to find which form to show and how to validate it. a little more complicated, but keeps my code on fewer pages and makes finding errors easier.

commented: Nice one +14

Yes, you were absolutely right. They seem to be working now! Thanks bunches!
~Amy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.