Session Clarification
Moderator: General Moderators
Session Clarification
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.
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.
- protokol
- Forum Contributor
- Posts: 353
- Joined: Fri Jun 21, 2002 7:00 pm
- Location: Cleveland, OH
- Contact:
Well in your case the way you would use sessions is like this:
In the later versions of PHP you will be able to do this:
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!
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']}";Code: Select all
session_start();
$_SESSIONї'var_name'] = "some value";
echo "The session variable named 'var_name' has this value: {$_SESSIONї'var_name']}";Hope this helps!
Last edited by protokol on Wed Jul 17, 2002 11:31 am, edited 1 time in total.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
One thing to watch out for:
A couple of people have posted problems that relate to that recently so I just thought I'd put it in.
Mac
From the session_register() man page.php manual wrote:If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister().
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.
Twiglemac says:
or you can't use these at all in the same code. The manual is a little fuzzy on this point.
Thanks.
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 thisYou can't use session_register(); with $HTTP_SESSION_VARS, according to the manual
Code: Select all
session_register($HTTP_SESSION_VARSї'index'];Thanks.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
From what I've seen of other people's problems this sort of thing:
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:
If register_globals is on, then you can use the above or session_register():
Mac
Code: Select all
session_register('user');
$HTTP_SESSION_VARSї'user'] = $username;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;
?>Code: Select all
<?php
session_start();
$user = $username;
session_register('user');
?>- protokol
- Forum Contributor
- Posts: 353
- Joined: Fri Jun 21, 2002 7:00 pm
- Location: Cleveland, OH
- Contact:
sorry this would be a better way to do this:
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 
Code: Select all
session_start();
$var_name = "some value";
session_register("var_name");
echo $HTTP_SESSION_VARSї'var_name'];- sam
- Forum Contributor
- Posts: 217
- Joined: Thu Apr 18, 2002 11:11 pm
- Location: Northern California
- Contact:
Protokol that is what is being frowned upon in the php community..
Try this:
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
Try this:
Code: Select all
session_start();
$HTTP_SESSION_VARSї'var_name'] = "some value";Cheers Sam
- protokol
- Forum Contributor
- Posts: 353
- Joined: Fri Jun 21, 2002 7:00 pm
- Location: Cleveland, OH
- Contact:
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.sam wrote:Protokol that is what is being frowned upon in the php community..
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.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
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
Code: Select all
session_start();
$HTTP_SESSION_VARSї'var_name'] = 'some value';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