Problem with session variables
Moderator: General Moderators
Problem with session variables
I don't know enough about sessions to know what the problem is. I just encountered it while reading a tutorial on sessions.
Here is my first page (where the user will enter his name):
page1.html:
<html>
<body>
<FORM METHOD="POST" ACTION="page2.php">
Enter your Name: <input type="text" name="name">
<input type="SUBMIT" value="Submit">
</FORM>
</body>
</html>
Here is second page where the form data goes:
page2.php:
<?php
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
echo "<strong>Step 2 - Register Session </strong><br />";
// Get the user's input from the form
$name = $_POST['name'];
// Create a new Session Value
session_register('name');
// Register the input with the value
$_SESSION['name'] = $name;
// Display the sssion information:
?>
Welcome to my website <strong><? echo $_SESSION['name']; ?></strong>!<br />
Let's see what happens on the <a href="page3.php">next page.</a><br /><br />
Here is third page:
page3.php:
<?php
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
?>
<strong>Step 3 - Test Session Part II </strong><br />
Hey <strong><? echo $_SESSION['name']; ?></strong> Everything is still working!<br /><br />
<strong>Pick an option:</strong><br />
Let's delete this session value now. <a href="page4.php">Click Here.</a><br />
Let's destroy this session. <a href="page5.php">Click Here.</a><br /><br />
The problem is on page 3 where it is supposed to print the session variable name. But it just leaves that out, like we never even inserted that into the code.
Any help with this?
Thanks a bunch for all help and advice provided.
Here is my first page (where the user will enter his name):
page1.html:
<html>
<body>
<FORM METHOD="POST" ACTION="page2.php">
Enter your Name: <input type="text" name="name">
<input type="SUBMIT" value="Submit">
</FORM>
</body>
</html>
Here is second page where the form data goes:
page2.php:
<?php
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
echo "<strong>Step 2 - Register Session </strong><br />";
// Get the user's input from the form
$name = $_POST['name'];
// Create a new Session Value
session_register('name');
// Register the input with the value
$_SESSION['name'] = $name;
// Display the sssion information:
?>
Welcome to my website <strong><? echo $_SESSION['name']; ?></strong>!<br />
Let's see what happens on the <a href="page3.php">next page.</a><br /><br />
Here is third page:
page3.php:
<?php
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
?>
<strong>Step 3 - Test Session Part II </strong><br />
Hey <strong><? echo $_SESSION['name']; ?></strong> Everything is still working!<br /><br />
<strong>Pick an option:</strong><br />
Let's delete this session value now. <a href="page4.php">Click Here.</a><br />
Let's destroy this session. <a href="page5.php">Click Here.</a><br /><br />
The problem is on page 3 where it is supposed to print the session variable name. But it just leaves that out, like we never even inserted that into the code.
Any help with this?
Thanks a bunch for all help and advice provided.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
-
Tubbietoeter
- Forum Contributor
- Posts: 149
- Joined: Fri Mar 14, 2003 2:41 am
- Location: Germany
Try to drop the comment on the first line of PHP code:
Not:
<?php
// start the session
session_start();
But:
<?php
session_start();
I believe the start-thing needs to be the first thing done, don't know if that is true for comments as well.
Also, try echo the value of $_POST['name'] to see if the problem is the form not the session.
Not:
<?php
// start the session
session_start();
But:
<?php
session_start();
I believe the start-thing needs to be the first thing done, don't know if that is true for comments as well.
Also, try echo the value of $_POST['name'] to see if the problem is the form not the session.
I don't think you need to start with session_start(), I always start all my pages with error_reporting() and have never had any issues with sessions.Tubbietoeter wrote:Try to drop the comment on the first line of PHP code:
Not:
<?php
// start the session
session_start();
But:
<?php
session_start();
I believe the start-thing needs to be the first thing done, don't know if that is true for comments as well.
Also, try echo the value of $_POST['name'] to see if the problem is the form not the session.
Turn on errorreporting and see what errors you get. This should help you big-time.nigma wrote:Even when I remove the comment and the session_register itdoes not work. I mean, the session variable can be viewed on the second page but not the third pgae. The second page is the page where it is declared.
