Page 1 of 1

Problem with session variables

Posted: Mon Mar 24, 2003 4:08 pm
by nigma
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.

Posted: Mon Mar 24, 2003 5:41 pm
by Adagio
I think the code looks very right.
However, is this a local windows system? I know that the default php.ini of php 4.3.1 has a error in it when it comes to sessions. It sets the session directory to /tmp. Maybe something isn't right in you php.ini file?

Posted: Mon Mar 24, 2003 6:05 pm
by nigma
I checked all those settings. The session dir is C:\Windows\temp and using that code I am able to print out the name I entered on the first page to the second page (by going <?php echo $_SESSION['name']; ?>. But the problem occurs when I try to do the same thing on the third page.

Posted: Mon Mar 24, 2003 6:24 pm
by phice
You don't need session_register('name');

Posted: Mon Mar 24, 2003 7:18 pm
by nigma
I just left that in there by accident. Originally it had the scripts without the session_register() dealy. I only added that when I was attempting to debug my code. In another sessions tutorial it said you needed that so I tried it out.

Posted: Tue Mar 25, 2003 4:45 am
by twigletmac
Does the code work when you remove the session_register() bit? session_register() and $_SESSION don't play nicely together.

Mac

Posted: Tue Mar 25, 2003 5:23 am
by Tubbietoeter
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.

Posted: Tue Mar 25, 2003 8:24 am
by ckuipers
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.
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.

Posted: Tue Mar 25, 2003 11:25 am
by nigma
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.

Posted: Wed Mar 26, 2003 3:16 am
by ckuipers
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.
Turn on errorreporting and see what errors you get. This should help you big-time.

Posted: Wed Mar 26, 2003 3:55 am
by nigma
Absolutely, Will do.

Thanks a bunch.