Page 1 of 1

Sessions in 4.2

Posted: Sun Oct 27, 2002 8:12 pm
by NNParksITGuy
Hey :D

Since I banged my head for a while figuring this out, I thought I'd pass it on in case someone else was having problems with sesssion handling in 4.2 This is outlined well in the online manual at http://www.php.net/

With register_globals off by default, use of the old session commands, such as

Code: Select all

session_register('variable name')
session_unregister('variable name')
session_is_registered('variable name')
is no longer recommended, and in some cases may not work.

Instead, session info is now available via the superglobal array $_SESSION

To use sessions, you still need to begin each script with

Code: Select all

session_start()
To register a variable into the session, use

Code: Select all

$_SESSIONї'variable name'] = 'value you want to set';
To access a session variable, refer to it with the same syntax

Code: Select all

$_SESSIONї'variable name']
For example, this scriptlet sets a variable into the session and then prints it to the screen:


Code: Select all

<?php
session_start();

$user = 'Mack Owens';
$_SESSIONї'valid_user'] = $user;
echo $_SESSIONї'valid user'];

?>
If you enter a variable into a session using session_register('variable name'), it will not be available in the $_SESSION array until the next time the session_start() command is issued. Variables added via the $_SESSION['variable name'] = 'value' method are available immediately.

Hope this helps someone out there.

Posted: Mon Oct 28, 2002 1:38 am
by Takuma
You can't mix 2 things together... That's it how PHP does it.

Posted: Mon Oct 28, 2002 1:33 pm
by qads
well that sloves my problem :D ..i think, will this work in older php version?

thanks

- Qads

Posted: Tue Oct 29, 2002 2:29 am
by twigletmac
Qads wrote:will this work in older php version?
Depends, in theory you can just replace $_SESSION with $HTTP_SESSION_VARS on PHP <= 4.0.6 but I know I've seen people have a lot of problems with this so you may have to use session_register() instead.

Mac

Posted: Tue Oct 29, 2002 8:24 am
by qads
dang...why can't they stick with one function name? grrrr

My Questions on session variable with PHP 4.2

Posted: Tue Oct 29, 2002 12:48 pm
by jmpan
A temporary file is created accordingly when using the session variable.
How can these junk files be deleted after web client quits the site?
I know that the session_destory() works very well.
But what if the client exits the web site by simply close the browser window?

Thanks for answering the question.