Jump to content

[SOLVED] Can't figure out sessions


JPark

Recommended Posts

I am trying to figure out sessions with 3 very simple pages and cannot seem to get it to work.

 

Page 1 has a form that sends 3 inputs to Page 2.  Page 2 has session variables and a link to Page 3.  Page 3 tries to read the form data (should fail) and the session data (should work).

 

What am I missing???  I've tried several different things...

 

Page 1:

<?php session_start(); ?>
<html>
<head>
<title>Sessions Test : Page 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
To test sessions...
<ol>
<li>Create a simple form</li>
<li>Send it to the second page.</li>  
	<ul>

    <li>Does it keep the form information? </li>
	</ul>
<li>Return to the first page.</li>
	<ul>
		<li>Does it keep the session information?</li>
		<li>What about any variables that were not stored?</li>
	</ul>    
<li>Go to a third page.</li>
			<ul>
		<li>Does it keep the session information?</li>
		<li>What about any variables that were not stored?</li>
	</ul>    
</ol>
<?

echo $_SESSION['color']," color<br />";
?>
<form action="page2.php" name="sessiontest" method="post" >
<p>Choose a color: <input name="color" type="text" size="12" maxlength="12"></p>
<p>Choose a size: <input name="size" type="text" size="12" maxlength="12"></p>
<p>Choose a shape: <input name="shape" type="text" size="12" maxlength="12"></p>
<input name="Submit" type="submit" value="Submit">
</form>
<p>Go to <a href="page2.php">Page 2</a></p>
<p>Go to <a href="page3.php">Page 3</a></p>

</body>
</html>

 

Page 2:

<? session_start(); ?>
<html>
<head>
<title>Sessions Test : Page 2</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>Page 2</p>
<?
if (isset($_SESSION['color'])) 
$color = $_SESSION['color'];
else 
	$color = "None found";
$shape = $_SESSION['shape'];
$size = $_SESSION['size'];

$color2 = $_POST['color'];
$shape2 = $_POST['shape'];
$size2 = $_POST['size'];

echo "SessionColor = ".$color."<br />";
echo "SessionShape = ".$shape."<br />";
echo "SessionSize = ".$size."<br /><br />";

echo "FormColor = ".$color2."<br />";
echo "FormShape = ".$shape2."<br />";
echo "FormSize = ".$size2."<br />";
?>

<p>Go back to <a href="page1.php">Page 1</a></p>
<p>Go to <a href="page3.php">Page 3</a></p>

</body>
</html>

 

Page 3:

<? session_start(); ?>
<html>
<head>
<title>Sessions Test : Page 3</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
if (isset($_SESSION['color'])) 
$color = $_SESSION['color'];
else 
	$color = "None found";
$shape = $_SESSION['shape'];
$size = $_SESSION['size'];

$color2 = $_POST['color'];
$shape2 = $_POST['shape'];
$size2 = $_POST['size'];

echo "SessionColor = ".$color."<br />";
echo "SessionShape = ".$shape."<br />";
echo "SessionSize = ".$size."<br /><br />";

echo "FormColor = ".$color2."<br />";
echo "FormShape = ".$shape2."<br />";
echo "FormSize = ".$size2."<br />";
?>

<p>Go back to <a href="page1.php">Page 1</a></p>
<p>Go back to <a href="page2.php">Page 2</a></p>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/151271-solved-cant-figure-out-sessions/
Share on other sites

I don't see anything that says:

$_SESSION['blah'] = 'blah';

 

You're never setting anything to the session. 

 

You might want to read through this: http://www.phpfreaks.com/tutorial/sessions-and-cookies-adding-state-to-a-stateless-protocol

Rats!  You beat me...

 

I went to http://www.smallbizonline.co.uk/php_session_variables.php and figured it out:

 

 

session_register ("color");	// Create a session variable called color
session_register ("size");				// Create a session variable called size
session_register ("shape");				// Create a session variable called shape
$HTTP_SESSION_VARS ["color"] = $color;	// Set name = form variable $color
$HTTP_SESSION_VARS ["size"] = $size;	// Set job = form variable $size
$HTTP_SESSION_VARS ["shape"] = $shape;	// Set job = form variable $shape

$color2=$_POST['color'];				// Grab the form variable called color
$size2=$_POST['size'];					// Grab the form variable called size
$shape2=$_POST['shape'];				// Grab the form variable called shape

echo "The session variable color is ".$color."<br />";
echo "The form variable color is ".$color2."<br /><br />";

echo "The session variable size is ".$size."<br />";
echo "The form variable size is ".$size2."<br /><br />";

echo "The session variable shape is ".$shape."<br />";
echo "The form variable shape is ".$shape2."<br /><br />";

 

Is this the easiest/best way to do it?

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.