PDA

View Full Version : questions about session


taydag
01-31-2004, 06:00 PM
Hi fiends

I have a plenty of questions regardin SESSION stuff... I appreciate your urgent and clear responses :)

1)Do i have to include session_start() command in all of my php pages in order to keep the user info and use them in the case of necessity?

2) Do i have to add the session id to the url as a query string for each page my memebrs visit?

3)Exactly what should i do to call a session variable ? Is $_session['variable'] == $variable
enough to call and use the variable?
Your sample codes to keep and use memebr details via sessions on my website would be highly appreciated.
Thank you for your responses by now :)

Spookster
01-31-2004, 06:26 PM
In order to start or access a session you need to have session_start(); called before sending any output to the browser. So yes in any page where you wish to access session data that must be at the top of the file. If you plan on using sessions throughout the site a good idea might be to create an include file that contains

session_start();

and any other code that needs to be included at the top of every file in the site. Then if you need to change something you would only need to change it in that one file.

No you don't need to add the session id to the URL for your members visits. PHP handles all of this for you. PHP will determine what the browser supports as far as cookies and will either store a server session cookie or append the session ID to the URL.

To create a session variable you simply need to declare it.

$_SESSION['variablename'];

To assign a value to it goes like this:

$_SESSION['variablename'] = "a value";

To access it such as printing out the value:

echo $_SESSION['variablename'];

Spookster
01-31-2004, 06:30 PM
There are plenty of tutorials that can elaborate more on sessions

http://www.phpfreaks.com/tutorials/41/0.php

taydag
02-01-2004, 08:25 PM
Thanks Spooskter :)

mrjamin
02-01-2004, 10:44 PM
Originally posted by taydag
3)Exactly what should i do to call a session variable ? Is $_session['variable'] == $variable
enough to call and use the variable?

well, yes and no. It all depends on the PHP configuration.

If register_globals is ON then yes, that'll work BUT its VERY insecure and very bad practice.

Spookster
02-02-2004, 12:06 AM
Originally posted by mrjamin
well, yes and no. It all depends on the PHP configuration.

If register_globals is ON then yes, that'll work BUT its VERY insecure and very bad practice.

That's not correct. That will work regardless if register_globals is set to on or not. $_SESSION[] is a superglobal therefore does not depend on the register_global setting. The only thing that would affect accessing the session variables in this way is if they are using a PHP version prior to 4.1.0 in which case you would use the superglobal $HTTP_SESSION_VARS[] which is now deprecated.

mrjamin
02-02-2004, 12:09 AM
Originally posted by Spookster
That's not correct. That will work regardless if register_globals is set to on or not. $_SESSION[] is a superglobal therefore does not depend on the register_global setting. The only thing that would affect accessing the session variables in this way is if they are using a PHP version prior to 4.1.0 in which case you would use the superglobal $HTTP_SESSION_VARS[] which is now deprecated.

i was talking with reference to using $variable as opposed to $_SESSION['variable'] - my bad, i wasn't too clear