Monday, June 19, 2006

Session in PHP mystery solved

As the problem that I posted yesterday, I finally found what went wrong in that particular thing. In PHP, wherever page that you need session, session_start() has to be called. This is solved as I include the auth.php in all my pages, yet the main problem of the session variable is the session_register(). The session_register() is used for older version of php (3.0 or below) and what I am using now is php5.1, which supports $_SESSION super variable. When you called your session_register(), the $_SESSION super variable would not work as what you expected. Thus by removing the session_register() and replace it with $_SESSION super variable, and whole things works as what I want. :)

Previous code:


auth.php
<?php
session_start(); //start session when your auto_start_session is 0 (off) in your php.ini
session_register($auth); //register global variable 'auth' into session variable
session_register($username); //register username as session variable too.
?>



Corrected code:


auth.php
<?php
session_start(); //start session when your auto_start_session is 0 (off) in your php.ini
$_SESSION['auth'] = $auth; //register global variable 'auth' into session variable
$_SESSION['username'] = $username; //register username as session variable too.
?>

0 comments: