note 9895 modified in ref.session by yohgaki

From: Date: Sat, 19 Jan 2002 06:28:34 +0000
Subject: note 9895 modified in ref.session by yohgaki
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-25100@lists.php.net to get a copy of this message
Just to make explicit what probably others have already figured out from the documentation, only global variables can be stored in the session. Any variable that you manipulate in a function must be declared global if you want to store it in the session, and any session variable that you need access to must be declared global. For example, session_start(); // Function to check login function confirmUserLogin ($postLoginUrl) { // To get access to this session var, // I must declare it global. global $session_userID; if (!IsSet($session_userID)) { session_register('session_post_login_page'); // To set this session var, I must // declare it global. global $session_post_login_page; $session_post_login_page = $postLoginUrl; redirect("/login/"); } } // Function to set a user's ID after login function setUserLogin ($userID) { // Again, must declare session_userID // global so that I can set it. global $session_userID; $session_userID = $userID; } // Function to retrieve the stored userID function getUserLogin () { // Again, must declare variable global // in order to access it. global $session_userID; return($session_userID); } Special thanks to p.fournier@ieee.ca's note for pointing me in the right direction. -- Editor's Note: Use of $HTTP_SESSION_VARS/$_SESSION is recommended. --was-- Just to make explicit what probably others have already figured out from the documentation, only global variables can be stored in the session. Any variable that you manipulate in a function must be declared global if you want to store it in the session, and any session variable that you need access to must be declared global. For example, session_start(); // Function to check login function confirmUserLogin ($postLoginUrl) { // To get access to this session var, // I must declare it global. global $session_userID; if (!IsSet($session_userID)) { session_register('session_post_login_page'); // To set this session var, I must // declare it global. global $session_post_login_page; $session_post_login_page = $postLoginUrl; redirect("/login/"); } } // Function to set a user's ID after login function setUserLogin ($userID) { // Again, must declare session_userID // global so that I can set it. global $session_userID; $session_userID = $userID; } // Function to retrieve the stored userID function getUserLogin () { // Again, must declare variable global // in order to access it. global $session_userID; return($session_userID); } Special thanks to p.fournier@ieee.ca's note for pointing me in the right direction. http://www.php.net/manual/en/ref.session.php

« previous php.notes (#25100) next »