Global Vars & possiblities of $_SESSION

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mythic Fr0st
Forum Contributor
Posts: 137
Joined: Sat Dec 02, 2006 3:23 am
Contact:

Global Vars & possiblities of $_SESSION

Post 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?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Global Vars & possiblities of $_SESSION

Post 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.
Mythic Fr0st
Forum Contributor
Posts: 137
Joined: Sat Dec 02, 2006 3:23 am
Contact:

cool

Post by Mythic Fr0st »

Alrighty, thanks, trying to see if I can it to end @ logout
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Post Reply