By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,824 Members | 2,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,824 IT Pros & Developers. It's quick & easy.

PHP Sessions

P: 12
I cant get PHP sessions to work.

I have a login page with this code:

session_start();
$_SESSION['userid'] = $userid;

echo $_SESSION['userid'];

When i access the page and enter my login details it runs through this piece of code and the echo statement prints out the userid as expected.

However when i then click an href to go to another page on my site I have the following code:

session_start();
$userid=$_SESSION['userid'];
echo $userid;

The echo statement returns nothing. Am i using sessions correctly or is there something more I need to do?

Cheers.
May 14 '07 #1
Share this Question
Share on Google+
4 Replies


P: 12
I am displaying the SID when i click between pages and the PHPSESSID numbers are different. Why would this happen?
May 14 '07 #2

Atli
Expert 5K+
P: 5,058
I am displaying the SID when i click between pages and the PHPSESSID numbers are different. Why would this happen?
This can happen if your PHP session configuration is incorrect. On some systems you must set the 'session.save_path'. You also might want to set the 'session.use_cookies' to 1.
May 14 '07 #3

P: 12
My web provider doesnt really offer much guidance but they said i could create php.ini files so i did. i added one to my webspace and set 'session.save_path' and the cookies indiciator to 1. I am getting various error messages when i run the scripts and i am certain the problem is the 'session.save_path' setting. I am on a Linux server. The book i have says set this to the C drive on Windows but that would be for a microsoft server i assume. Can anyone give me some pointers to what i should be setting the 'session.save_path' to?

Cheers.
May 18 '07 #4

P: 6
rug
My web provider doesnt really offer much guidance but they said i could create php.ini files so i did. i added one to my webspace and set 'session.save_path' and the cookies indiciator to 1. I am getting various error messages when i run the scripts and i am certain the problem is the 'session.save_path' setting. I am on a Linux server. The book i have says set this to the C drive on Windows but that would be for a microsoft server i assume. Can anyone give me some pointers to what i should be setting the 'session.save_path' to?

Cheers.
Here's some info from http://au3.php.net/session that might help you:
session.save_path string

session.save_path defines the argument which is passed to the save handler. If you choose the default files handler, this is the path where the files are created. Defaults to /tmp. See also session_save_path().

There is an optional N argument to this directive that determines the number of directory levels your session files will be spread around in. For example, setting to '5;/tmp' may end up creating a session file and location like /tmp/4/b/1/e/3/sess_4b1e384ad74619bd212e236e52a5a174If . In order to use N you must create all of these directories before use. A small shell script exists in ext/session to do this, it's called mod_files.sh. Also note that if N is used and greater than 0 then automatic garbage collection will not be performed, see a copy of php.ini for further information. Also, if you use N, be sure to surround session.save_path in "quotes" because the separator (;) is also used for comments in php.ini.
Warning

If you leave this set to a world-readable directory, such as /tmp (the default), other users on the server may be able to hijack sessions by getting the list of files in that directory.

Hope this helps,
rug
May 18 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.