954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Session variables not carrying over to next page

Howdy all,

I'm making an authentication script using sessions variables, but the varible seems to disappear when trying to access it from another page.

A simple form on the home page takes a user name and password, it then goes to an authentication page. The authenticate page is making a match and setting the session variables. I have tested this part and know that for sure. The authentication page then redirects in 2 seconds back to the originating page by a meta refresh. In this case, its the index page, but the index page does not recognize the session variable. Here is the code on the index page:

<strong>if</strong> <strong>($_SESSION[</strong>"authenticated"<strong>]==</strong>"YES"<strong>)</strong>
<strong>{</strong>
echo "Hi " <strong>.$_SESSION[</strong>"user_name"<strong>];</strong>
<strong>}</strong>
<strong>else</strong>
<strong>{</strong>
?>

HTML form code here.

<?php
<strong>}</strong>
?>
It supposed to acknowledge the user, or put the log in form on the page, but it doesn't recognize the varible. Can anyone help? I've googled and looked at the code over and over, but can't see anything wrong.


Also, I'm using PHP 4.2.?, so I should not have to register the session variables as recommended for version 4.1 and above, correct?




Thanks,

Gil

gil857
Light Poster
35 posts since Jun 2006
Reputation Points: 11
Solved Threads: 0
 

did you put session_start(); at the beginning of the second page

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

yeah,and try to echo the session to check if it exists

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

Yes, I did have the

session_start();


and it does show the session variables on the authentication page when I echo the

$_SESSION["authenticated"]

variable. It returns 'YES', so I know its making the query on the mySQL database and matching with the submitted data.

When I redirect back to the index page, it will not echo the authenticated or user_name variable.

gil857
Light Poster
35 posts since Jun 2006
Reputation Points: 11
Solved Threads: 0
 

you should have session_start(); on the index page as well so you can access the session values.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

Thanks! That did it!

I'm more familiar with ASP session variables because that's what I use at work. This was the first time I have used a PHP session script, I though you only needed the session_start() once.

Much thanks, I have spent way too much time trying to figure out what was wrong!

gil857
Light Poster
35 posts since Jun 2006
Reputation Points: 11
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You