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?
Global Vars & possiblities of $_SESSION
Moderator: General Moderators
-
Mythic Fr0st
- Forum Contributor
- Posts: 137
- Joined: Sat Dec 02, 2006 3:23 am
- Contact:
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
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;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
see http://de2.php.net/language.variables.scopeMythic Fr0st wrote:Firstly, how do I declare global variables?
O_O I would guess $var is a local variable...
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.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)
take a look at session.cookie_lifetime, session.gc_maxlifetime and session.gc_probability on http://de2.php.net/session
Almost infinite possiblities.Mythic Fr0st wrote:Q2:what kind of possibilities are there using it? preferably with logging in
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 wrote:Q3: I have session_start() on every page, is that bad?
-
Mythic Fr0st
- Forum Contributor
- Posts: 137
- Joined: Sat Dec 02, 2006 3:23 am
- Contact:
cool
Alrighty, thanks, trying to see if I can it to end @ logout
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
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.