Problem with session variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Problem with session variables

Post 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.
Adagio
Forum Newbie
Posts: 3
Joined: Mon Mar 24, 2003 5:26 pm

Post 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?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

You don't need session_register('name');
Image Image
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Does the code work when you remove the session_register() bit? session_register() and $_SESSION don't play nicely together.

Mac
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post 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.
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post 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.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post 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.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Absolutely, Will do.

Thanks a bunch.
Post Reply