Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session Problem - This should be easy but since I'm a noob ... 1

Status
Not open for further replies.

FNBIT

IS-IT--Management
Oct 27, 2006
74
US
Hello All,

I am starting to play with PHP again. It has been a long while since I have done this. My problem is this: I have a page that assigns a session variable like this
$_SESSION['EMail']= $row->Email;

I then have another page that is accessed via two header("location: settings. To ellaborate a little on this the first page takes a persons email and password to see if it is valid. Once it is, it then uses the header("location: to in an if statement to send the person to the main password protected page. At the top of this page I check to see if the password is set to the defualt password, if it is I use a second header("location: to send the person to a page to change the password.

This all works but when I end up on the password change page the session variable seems to have vanished. I have the following code in the system to check it:
Current EMail: <?php echo $_SESSION['EMail']; ?>
This seems to show the variable as either blank or NULL.

If I go to the server where the sessions are stored and open the session up using notepad, I can see the email. So it seems like the session is started but I am loosing the connection to it or something similar. Any ideas on what I am doing wrong?

Thanks!
 
you have to start the session first.
Code:
session_start();

or if you want to test for an existing session

Code:
if (session_id() == '') session_start();

 
Sorry, I guess I should have noted that I did start it. That is why I am seeing results on the server session directory. It all looks good until I try to pull up the variable. Apparently it is being stored correctly. When I tried a Google search it seems like people are having issues with saving the info. Since I can see this on the server that is not my issue.

However I will check to see if hte session_id is blank. That would at least let me know if I am loosing the session somehow.
 
are you testing on a local server? if so you could get a race condition on the server session file. I discuss this (briefly) on my site here

the trick is to add this line at the end of your script

Code:
session_write_close();
 
Thanks for that info. I will take a look at it but I found out what was wrong. I thought you had to have one page start the session. Apparently every page needs to have this. I thought if I typed session_start(); it would reset all the data and start a new session. Apparently this is not the way it works.

Therefore instructions should read (in the book I have and the other sites I visited):
At the very first line of EVERY page you want to use the session variables, you must type in session_start();.

So you were correct in your first response. Thank you.
 
depends what you mean by page. but it is true that for each browser-web interaction there needs to be a session_start().

 
To explain it further. I have three pages I am working with.

index.php
home.php
changepassword.php

The only way the system works is by having the session_start() at the top of each page. When I started I only had it on the index.php since that is where the person would start. That page would then call home.php with a header(location: command. If I did not have the session_start() at the top of the home.php also, the sessions variables where not available. ditto with the changepassword.php page.

I take it this is the way it is supposed to work?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top