Sessions in 4.2

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
NNParksITGuy
Forum Newbie
Posts: 4
Joined: Sat Oct 26, 2002 6:39 pm
Location: Newport News Virginia

Sessions in 4.2

Post 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.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

You can't mix 2 things together... That's it how PHP does it.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

well that sloves my problem :D ..i think, will this work in older php version?

thanks

- Qads
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

dang...why can't they stick with one function name? grrrr
jmpan
Forum Newbie
Posts: 1
Joined: Tue Oct 29, 2002 12:48 pm

My Questions on session variable with PHP 4.2

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