Confused object in session variable.
| From: | Steve Cayford | Date: | Thu, 25 Oct 2001 17:07:12 +0000 |
| Subject: | Confused object in session variable. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-72306@lists.php.net to get a copy of this message | ||
Well, it's probably me that's confused. I have an authenticate() function which should start a session and if the user is not logged in then show the login screen otherwise return after storing and registering a user object in a session variable. This object has accessor methods to get the login name, access level, etc... This seems to work okay--within the authenticate function I can access the object in the HTTP_SESSION_VARS array. But as soon as I return to the main script it's gone. I must be doing something wrong with the scoping, but I can't see what. Any thoughts? Here's the code:
<?php
/* --------- */
/* index.php */
/* --------- */
require_once('ucautho/ucautho.inc');
authenticate();
print("From index testUser: " . $testUser->getLogin() . "<br>\n");
print("From index HTTP_SESSION_VARS['ucAuthoUser']: ");
print($HTTP_SESSION_VARS['ucAuthoUser']->getLogin() . "<br>\n"); //this is line 12
?>
<?php
/* ------------------- */
/* ucautho/ucautho.inc */
/* ------------------- */
function authenticate($appName="") {
global $HTTP_SESSION_VARS, $HTTP_POST_VARS;
global $testUser;
session_name("UCAutho");
session_start();
if (isset($HTTP_SESSION_VARS['ucAuthoUser']) && $HTTP_SESSION_VARS['ucAuthoUser']->isValid()) {
return;
} else {
if (isset($HTTP_POST_VARS['authoSubmit'])) {
$HTTP_SESSION_VARS['ucAuthoUser'] =&
new UcAuthoUser($HTTP_POST_VARS['authoLogin'],$HTTP_POST_VARS['authoPword']);
if ($HTTP_SESSION_VARS['ucAuthoUser']->isValid()) {
session_register('ucAuthoUser');
$testUser = $HTTP_SESSION_VARS['ucAuthoUser'];
print("From authenticate testUser: " . $testUser->getLogin() . "<br>\n");
print("From authenticate HTTP_SESSION_VARS['ucAuthoUser']: ");
print($HTTP_SESSION_VARS['ucAuthoUser']->getLogin() . "<br>\n");
return;
}
}
showLogin($appName);
}
}
/* more functions and the class declaration snipped */
?>
Here's what I get when I login as 'steve' with a good password:
From authenticate testUser: steve
From authenticate HTTP_SESSION_VARS['ucAuthoUser']: steve
From index testUser: steve
From index HTTP_SESSION_VARS['ucAuthoUser']:
Fatal error: Call to a member function on a non-object in /home/httpd/html/ucdamage/index.php on line 12
Note the testUser works in both instances, the session var only works inside the function.
-Steve