Forum Moderators: coopster

Message Too Old, No Replies

$_SESSION variable not being set

         

crustymonkey

5:27 am on Jun 23, 2004 (gmt 0)

10+ Year Member



I have been flogging myself with this problem now for hours with no results.

The problem I am having is with a cookied login system that I have created. When someone comes to the first page of the site, I call a method from my login class that validates a user via the information stored in a cookie and returns true if authenticated and false otherwise. My problem lies in this: the cookie authentication is returning true and I then set a $_SESSION variable equal to the user's login id. The next step is a simple header() redirect to the home page. All works except for the $_SESSION variable is not set when the home page loads up. I can't figure this one out.

<?php

session_start ();
define ("INCDIR" , "includes/");
require_once (INCDIR . "common.php");
require_once (INCDIR . "login.class.php");

$login = new login ($_SESSION["userid"]);

if ($login->authenticated) {
header ("Location: home.php");
}
elseif ($login->cookie_auth ($_COOKIE["tcAuth"])) {
$_SESSION["userid"] = $login->userid;
header ('Location: home.php');
}

That is the top of the php index page with the redirect code.

Thanks for any help. If you need more info, let me know.

tomda

7:44 am on Jun 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To me, it seems your forgot to register the Session variable into your session.

Something like this for example :

session_register('userid');
$_SESSION['userid'] = $user_id;

Tommy

pmdung

8:43 am on Jun 23, 2004 (gmt 0)

10+ Year Member



to tomda: It is not necessary
to crustymonkey: is there session_start() at begin of home.php?

crustymonkey

12:33 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



Yeah, session_start() is at the top of home.php.

pmdung

1:33 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



I don't know what's wrong. But you should disable somme lines and try with new code, perhaps you will see where is the error code.

crustymonkey

3:12 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



Finally figured this one out. The next few lines of code below what I have there are these:

if ($login->authenticate ($_POST["username"] , $_POST["password"] , false) && $_POST["cookied"]) {
$login->set_cookie ();
}

What was happening is that the $_POST vars weren't set here and I didn't have the authenticate() method set up so that the username and password variables being passed to it were optional. Therefore, that method was throwing an error, an error that I never saw because the redirect would take me directly to the next page without setting the $_SESSION var. Basically, a little FYI for anyone else.