Page 1 of 1
Session Clarification
Posted: Tue Jul 16, 2002 9:54 pm
by EricS
I'm searching through php.net's online manual and having a hard time figuring out the difference between: session_register(variable), $_SESSION[array_index], and $HTTP_SESSION_VARS[array_index].
In particular: what is the difference, which versions each can be used with, and when is it best to use one over the others. To add a tad bit more complication, I will be using session variables in an unsecure mode first (HTTP://) then in a secure mode later (HTTPS://). I will need the information to transfer from unsecure to secure and remain in contact.
I'm using PHP 4.0.6. Unfortunately I can't upgrade till Sun gets off it's fat duff and sends out a patch to update it's RAQ servers.
I know there has been alot of discussion about sessions lately and I do appologize for egging it on even further.
Posted: Tue Jul 16, 2002 10:10 pm
by protokol
Well in your case the way you would use sessions is like this:
Code: Select all
session_start(); // allow session variables to be used
$var_name = "some value";
session_register("var_name");
echo "The session variable named 'var_name' has this value: {$HTTP_SESSION_VARSї'var_name']}";
In the later versions of PHP you will be able to do this:
Code: Select all
session_start();
$_SESSIONї'var_name'] = "some value";
echo "The session variable named 'var_name' has this value: {$_SESSIONї'var_name']}";
Notice that in the latter version the session_register() function is not needed. You can simply manipulate the $_SESSION array just like you would any other normal array.
Hope this helps!
Posted: Wed Jul 17, 2002 5:14 am
by twigletmac
One thing to watch out for:
php manual wrote:If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister().
From the
session_register() man page.
A couple of people have posted problems that relate to that recently so I just thought I'd put it in.
Mac
This is the contradiction I'm trying to work out.
Posted: Wed Jul 17, 2002 9:16 am
by EricS
Twiglemac says:
You can't use session_register(); with $HTTP_SESSION_VARS, according to the manual
And yet Protokol clearly uses session_start and session_register then $HTTP_SESSION_VARS. Is the manual saying you can't use $HTTP_SESSION_VARS as a variable within session_register like this
Code: Select all
session_register($HTTP_SESSION_VARSї'index'];
or you can't use these at all in the same code. The manual is a little fuzzy on this point.
Thanks.
Posted: Wed Jul 17, 2002 10:55 am
by twigletmac
From what I've seen of other people's problems this sort of thing:
Code: Select all
session_register('user');
$HTTP_SESSION_VARSї'user'] = $username;
causes issues. You really need to decide on one or the other. Which you use is up to you but is affected by whether register_globals is on or off.
If register_globals is off then session_register() will not work so you would use something like the following:
Code: Select all
<?php
session_start();
$HTTP_SESSION_VARSї'user'] = $username;
?>
If register_globals is on, then you can use the above or session_register():
Code: Select all
<?php
session_start();
$user = $username;
session_register('user');
?>
Mac
Posted: Wed Jul 17, 2002 11:30 am
by protokol
sorry this would be a better way to do this:
Code: Select all
session_start();
$var_name = "some value";
session_register("var_name");
echo $HTTP_SESSION_VARSї'var_name'];
Initialize the variable with a value BEFORE registering it. I edited my previous post to show this. Sorry for confusing you! I need to double-check my posts from now on

Posted: Wed Jul 17, 2002 7:01 pm
by sam
Protokol that is what is being frowned upon in the php community..
Try this:
Code: Select all
session_start();
$HTTP_SESSION_VARSї'var_name'] = "some value";
That is the optimal way to register sessions, if you don't want to assign a value right away just assign a 0... (an you have use $_SESSION the same way)
Cheers Sam
Posted: Wed Jul 17, 2002 7:07 pm
by protokol
sam wrote:Protokol that is what is being frowned upon in the php community..
Well if he's looking for the session_register() clarification .. (for a 4.0.6 server), then I believe that my message is just fine for him.
Obviously if he is using the newer versions of PHP, then he needs to learn $_SESSION. I don't have troubles with them. I am very proficient in this language.
Posted: Thu Jul 18, 2002 1:49 am
by twigletmac
It looks like session_register() is being phased out (mainly because it won't work with register_globals off) so for future proofing it would probably be best to use the
Code: Select all
session_start();
$HTTP_SESSION_VARSї'var_name'] = 'some value';
way of dealing with session variables. It'll make it easier to port your code when you upgrade your server and doesn't require register_globals to be on so is of more use to others who may want to use your code.
None of which has to do with anybody's coding proficiency of course, more with the fact that what was once considered the accepted way of working with sessions is no longer.
Mac