Re: [PHP] Session Variables in a Class
Thanks Curt, You were absolutely correct. Removing the session_register() lines before the $_SESSION[] fixed the problem. By mixing the two before, it actually would register the variable so I could later test for the registered session variable but it did not retain the value I assigned it. Thanks again for the help. Mike * Thus wrote micro brew ([EMAIL PROTECTED]): Hi everyone, I've been experimenting with sessions. I can make them work just fine in a normal PHP page. But when I create a class and try to define a session variable within a method in the class it won't work. I've tried setting the session variable directly ($_SESSION['name']='somevalue') but that didn't work. The normal way of setting it - session_start(); session_register('name'); $name='somevalue'; You might want to read http://php.net/session You are most likley running into a register_globals issue. A couple things to note: . You must not mix $_SESSION[] access with session_register() anywhere in your script. . When you issue session_register('name') inside your class it is declaring the global var $name as a session var, not the $name in your script. Curt __ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Session Variables in a Class
From: micro brew [EMAIL PROTECTED] Here's my class. I start the session in the constructor and in later methods attempt to register session variables. You can see I've tried a couple different methods but none of them maintain the session variable values. Any suggestions? Is register_globals ON or OFF? You shouldn't mix session_register() with $_SESSION. Just use one other the other, but I recommend just using $_SESSION. $_SESSION['var'] = 'some value'; to set a session var, and if(isset($_SESSION['var'])) to see if a variable is set. Also, if you have a varible $foobar within a class method that you define as a session var, you must make it global if you want to access it in other methods. The same thing for HTTP_SESSION_VARS. I think you're just running into a scope issue. ---John Holmes... Mike ?php class login { //This variable tells the browser where to redirect to the loginpage var $loginPage='http://www.somedomain.com/somepage.php'; var $exitPage='http://www.somedomain.com/somepage.html'; var $loggedIn; var $access; var $accesslevel; function login($loggedIn, $access, $accesslevel) { session_start(); if ($loggedIn=='1') { $this-checkLogin(); } if ($access=='1') { $this-checkAccess(); } if ($access=='0' $loggedIn=='0') { $this-authenticateLogin(); } } function checkLogin() { if (session_is_registered('user')!='True') { session_register('unauthorizedaccess'); $unauthorizedaccess=http://; . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']; header(location: $this-loginPage); exit(); } } function checkAccess() { if (session_is_registered('permission')=='True') { if ($HTTP_SESSION_VARS['permission']$this-accesslevel) { session_destroy(); header(location: $this-exitPage); exit(); } } else { session_register('unauthorizedaccess'); $unauthorizedaccess=http://; . $_SERVER[SERVER_NAME] . $_SERVER[SCRIPT_NAME]; header(location: $this-loginPage); exit(); } } function authenticateLogin() { if ((!$HTTP_POST_VARS['un']) || (!$HTTP_POST_VARS['pw'])) { session_register('loginError'); header(Location: $this-loginPage); exit(); } else { include(includes/db_connect.inc); //call db_connect function from db_connect.inc db_connect() or trigger_error(MySQL error # . mysql_errno() . : . mysql_error()); $query=select * from users where username=\ . addslashes($HTTP_POST_VARS['un']) .\ and password=\ . addslashes($HTTP_POST_VARS['pw']) . \; $result=mysql_query($query) or trigger_error(MySQL error # . mysql_errno() . : . mysql_error()); $foundCount = mysql_num_rows($result); if ($foundCount==1) { session_register('user'); session_register('permission'); session_register('company'); session_unregister('loginError'); for ($i=0; $i $foundCount; $i++) { $row=mysql_fetch_array($result); $_SESSION['user']=$row['userID']; $_SESSION['permission']=$row['permissionLevel']; $_SESSION['company']=$row['companyID']; } if (session_is_registered('unauthorizedaccess')=='True') { $location=$HTTP_SESSION_VARS['unauthorizedaccess']; session_unregister('unauthorizedaccess'); header(Location: $location); exit(); } } else { session_register('loginError'); header(Location: $this-loginPage); exit(); } } } //closes class } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Session Variables in a Class
Hi everyone, I've been experimenting with sessions. I can make them work just fine in a normal PHP page. But when I create a class and try to define a session variable within a method in the class it won't work. I've tried setting the session variable directly ($_SESSION['name']='somevalue') but that didn't work. The normal way of setting it - session_start(); session_register('name'); $name='somevalue'; - didn't work either (within the class). Am I just missing something obvious? Any suggestions? TIA. Mike __ Do you Yahoo!? Yahoo! SiteBuilder - Free, easy-to-use web site design software http://sitebuilder.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Session Variables in a Class
Am Die, 2003-09-09 um 10.19 schrieb micro brew: Hi everyone, I've been experimenting with sessions. I can make them work just fine in a normal PHP page. But when I create a class and try to define a session variable within a method in the class it won't work. I've tried setting the session variable directly ($_SESSION['name']='somevalue') but that didn't work. The normal way of setting it - session_start(); session_register('name'); $name='somevalue'; - didn't work either (within the class). Am I just missing something obvious? Any suggestions? You have to include the class-definition _before_ you start the session. Read about it in the manual: http://www.php.net/manual/en/language.oop.serialization.php Hope that helps. -- Marco TIA. Mike __ Do you Yahoo!? Yahoo! SiteBuilder - Free, easy-to-use web site design software http://sitebuilder.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Session Variables in a Class
From: micro brew [EMAIL PROTECTED] I've been experimenting with sessions. I can make them work just fine in a normal PHP page. But when I create a class and try to define a session variable within a method in the class it won't work. I've tried setting the session variable directly ($_SESSION['name']='somevalue') but that didn't work. The normal way of setting it - session_start(); session_register('name'); $name='somevalue'; - didn't work either (within the class). didn't work isn't very helpful to us. How useful would it be for me to just answer you with it worked for me. Show us some code examples (short ones) as to how this doesn't work, please. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Session Variables in a Class
Here's my class. I start the session in the constructor and in later methods attempt to register session variables. You can see I've tried a couple different methods but none of them maintain the session variable values. Any suggestions? Mike ?php class login { //This variable tells the browser where to redirect to the loginpage var $loginPage='http://www.somedomain.com/somepage.php'; var $exitPage='http://www.somedomain.com/somepage.html'; var $loggedIn; var $access; var $accesslevel; function login($loggedIn, $access, $accesslevel) { session_start(); if ($loggedIn=='1') { $this-checkLogin(); } if ($access=='1') { $this-checkAccess(); } if ($access=='0' $loggedIn=='0') { $this-authenticateLogin(); } } function checkLogin() { if (session_is_registered('user')!='True') { session_register('unauthorizedaccess'); $unauthorizedaccess=http://; . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']; header(location: $this-loginPage); exit(); } } function checkAccess() { if (session_is_registered('permission')=='True') { if ($HTTP_SESSION_VARS['permission']$this-accesslevel) { session_destroy(); header(location: $this-exitPage); exit(); } } else { session_register('unauthorizedaccess'); $unauthorizedaccess=http://; . $_SERVER[SERVER_NAME] . $_SERVER[SCRIPT_NAME]; header(location: $this-loginPage); exit(); } } function authenticateLogin() { if ((!$HTTP_POST_VARS['un']) || (!$HTTP_POST_VARS['pw'])) { session_register('loginError'); header(Location: $this-loginPage); exit(); } else { include(includes/db_connect.inc); //call db_connect function from db_connect.inc db_connect() or trigger_error(MySQL error # . mysql_errno() . : . mysql_error()); $query=select * from users where username=\ . addslashes($HTTP_POST_VARS['un']) .\ and password=\ . addslashes($HTTP_POST_VARS['pw']) . \; $result=mysql_query($query) or trigger_error(MySQL error # . mysql_errno() . : . mysql_error()); $foundCount = mysql_num_rows($result); if ($foundCount==1) { session_register('user'); session_register('permission'); session_register('company'); session_unregister('loginError'); for ($i=0; $i $foundCount; $i++) { $row=mysql_fetch_array($result); $_SESSION['user']=$row['userID']; $_SESSION['permission']=$row['permissionLevel']; $_SESSION['company']=$row['companyID']; } if (session_is_registered('unauthorizedaccess')=='True') { $location=$HTTP_SESSION_VARS['unauthorizedaccess']; session_unregister('unauthorizedaccess'); header(Location: $location); exit(); } } else { session_register('loginError'); header(Location: $this-loginPage); exit(); } } } //closes class } ? --- CPT John W. Holmes [EMAIL PROTECTED] wrote: From: micro brew [EMAIL PROTECTED] I've been experimenting with sessions. I can make them work just fine in a normal PHP page. But when I create a class and try to define a session variable within a method in the class it won't work. I've tried setting the session variable directly ($_SESSION['name']='somevalue') but that didn't work. The normal way of setting it - session_start(); session_register('name'); $name='somevalue'; - didn't work either (within the class). didn't work isn't very helpful to us. How useful would it be for me to just answer you with it worked for me. Show us some code examples (short ones) as to how this doesn't work, please. ---John Holmes... __ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Session Variables in a Class
* Thus wrote micro brew ([EMAIL PROTECTED]): Hi everyone, I've been experimenting with sessions. I can make them work just fine in a normal PHP page. But when I create a class and try to define a session variable within a method in the class it won't work. I've tried setting the session variable directly ($_SESSION['name']='somevalue') but that didn't work. The normal way of setting it - session_start(); session_register('name'); $name='somevalue'; You might want to read http://php.net/session You are most likley running into a register_globals issue. A couple things to note: . You must not mix $_SESSION[] access with session_register() anywhere in your script. . When you issue session_register('name') inside your class it is declaring the global var $name as a session var, not the $name in your script. Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Session Variables in a Class
* Thus wrote Curt Zirzow ([EMAIL PROTECTED]): * Thus wrote micro brew ([EMAIL PROTECTED]): Hi everyone, I've been experimenting with sessions. I can make them work just fine in a normal PHP page. But when I create a class and try to define a session variable within a method in the class it won't work. I've tried setting the session variable directly ($_SESSION['name']='somevalue') but that didn't work. The normal way of setting it - session_start(); session_register('name'); $name='somevalue'; You might want to read http://php.net/session You are most likley running into a register_globals issue. A couple things to note: . You must not mix $_SESSION[] access with session_register() anywhere in your script. . When you issue session_register('name') inside your class it is declaring the global var $name as a session var, not the $name in your script. ^^ should be 'function' Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php