note 9895 added to Session handling functions

From: Date: Thu, 30 Nov 2000 02:09:04 +0000
Subject: note 9895 added to Session handling functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-8286@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. http://www.php.net/manual/ref.session.php

« previous php.notes (#8286) next »