Page 1 of 1
Global Vars & possiblities of $_SESSION
Posted: Wed Dec 06, 2006 3:38 am
by Mythic Fr0st
Firstly, how do I declare global variables?
O_O I would guess $var is a local variable...
Second, SESSIONS
Q1: how long after being off the site, does $_SESSION expire? (IE when you leave the site, how long till it resets)
Q2:what kind of possibilities are there using it? preferably with logging in
Q3: I have session_start() on every page, is that bad?
Posted: Wed Dec 06, 2006 4:17 am
by CoderGoblin
Code: Select all
$GLOBALS['myvar']="This is myvar value // a global variables.
define('MYVAR','This is myvar value'); // a global constant
// to output
echo $GLOBALS['myvar'];
echo MYVAR;
Note the use of globals is often considered bad practice.
A1: Normally the session expires on the close of the browser. There are a couple of INI variables which can change this (
Session Handling Functions) as well as destroying the session via code (on logout).
A2: Normally you simply store the user_id and when you need to check if a user has logged in you can simply check if the $_SESSION['user_id'] is not empty. You could also store other things such as user preferences or other things which are frequently used but can be loaded once as they never change. These things could be loaded at login or using a method like...
Code: Select all
function getCats()
{
if (empty($_SESSION['categories']) {
// Load the categories into the session from the database
}
return $_SESSION['categories'];
}
A3: You need session_start on every page if you are using sessions.
Re: Global Vars & possiblities of $_SESSION
Posted: Wed Dec 06, 2006 4:18 am
by volka
Mythic Fr0st wrote:Firstly, how do I declare global variables?
O_O I would guess $var is a local variable...
see
http://de2.php.net/language.variables.scope
Mythic Fr0st wrote:Q1: how long after being off the site, does $_SESSION expire? (IE when you leave the site, how long till it resets)
You can write your own sesion handler but with the default mechanism: As long as the session file is present and the right session id is sent by the client the session can be reactivated.
take a look at session.cookie_lifetime, session.gc_maxlifetime and session.gc_probability on
http://de2.php.net/session
Mythic Fr0st wrote:Q2:what kind of possibilities are there using it? preferably with logging in
Almost infinite possiblities.
Mythic Fr0st wrote:Q3: I have session_start() on every page, is that bad?
Maybe it's a good idea. You need to start the session mechanism for each request you want to use session data with.
cool
Posted: Wed Dec 06, 2006 4:38 am
by Mythic Fr0st
Alrighty, thanks, trying to see if I can it to end @ logout
Posted: Wed Dec 06, 2006 4:44 am
by CoderGoblin
Normally on logout i simply use
session_regenerate_id(true); which creates a new session. I don't really care about the fact this new session is stored as it would get deleted on browser close and does not contain any user information.