timbrown Posted February 21, 2007 Share Posted February 21, 2007 Hello, I've got a problem with sessions. Sessions work fine on one page, but when I navigate to another page they don't appear to be set. I'm sure there's somethign simple i'm doing wrong but I've been trawling around for some time now and got nowhere. Any help would be great! Here's the code simplified - page 1... works fine <?php session_start(); $_SESSION["name"] = "tim"; echo $_SESSION["name"]; ?> <a href="page2">go there</a> page 2... says the session variable is not set <?php echo $_SESSION["name"]; ?> Quote Link to comment Share on other sites More sharing options...
hvle Posted February 21, 2007 Share Posted February 21, 2007 check the session_save_path variable in your php.ini. Sometime it pointed to an invalid directory path and caused session not working. Set it to a valid directory with write permission. if it still doesn't work, please say so Quote Link to comment Share on other sites More sharing options...
tcollie Posted February 21, 2007 Share Posted February 21, 2007 Shouldn't session_start(); be on each page that the session variables will be called on? Do you have it included on page_2? Quote Link to comment Share on other sites More sharing options...
timbrown Posted February 21, 2007 Author Share Posted February 21, 2007 check the session_save_path variable in your php.ini. Sometime it pointed to an invalid directory path and caused session not working. Set it to a valid directory with write permission. if it still doesn't work, please say so Hi, thanks for the reply. my session.save_path points to c:\php\sessiondata and there are session files in there. I did a test and I can see when I refresh page1, a new file is made. Quote Link to comment Share on other sites More sharing options...
timbrown Posted February 21, 2007 Author Share Posted February 21, 2007 Shouldn't session_start(); be on each page that the session variables will be called on? Do you have it included on page_2? I have tried that too, but it then says that the 'name' variable is not set. Does session_start() begin a new session each time? Quote Link to comment Share on other sites More sharing options...
hvle Posted February 21, 2007 Share Posted February 21, 2007 I have tried that too, but it then says that the 'name' variable is not set. Does session_start() begin a new session each time? Obviously I have missed out the very basic of session. You must called session_start() at the very beginning of all the scripts that use $_SESSION. Another important point you need to know is placing the php script tag '<?php' at the first character of the first line of the .php file. Meaning no any character, space ..etc before it. Quote Link to comment Share on other sites More sharing options...
timbrown Posted February 21, 2007 Author Share Posted February 21, 2007 I have tried that too, but it then says that the 'name' variable is not set. Does session_start() begin a new session each time? Obviously I have missed out the very basic of session. You must called session_start() at the very beginning of all the scripts that use $_SESSION. Another important point you need to know is placing the php script tag '<?php' at the first character of the first line of the .php file. Meaning no any character, space ..etc before it. Hi, still no joy. my 2 files now look like this - page 1 <?php session_start(); $_SESSION["name"] = "tim"; ?> <html> <a href="page2.php">go</a> </html> page 2 <?php session_start(); echo $_SESSION["name"]; ?> and I still get the message: Undefined index: name. any new ideas? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 21, 2007 Share Posted February 21, 2007 What OS & webserver are you using? I have seen mention of problems with sessions and IIS in the past. Ken Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 21, 2007 Share Posted February 21, 2007 Try using single quotes as well as using print_r to see if session data is there. page 1 <?php session_start(); $_SESSION['name'] = "tim"; print_r($_SESSION); ?> <html> <a href="page2.php">go[/url] </html> page 2 <?php session_start(); echo $_SESSION['name']; print_r($_SESSION); ?> Quote Link to comment Share on other sites More sharing options...
timbrown Posted February 21, 2007 Author Share Posted February 21, 2007 What OS & webserver are you using? I have seen mention of problems with sessions and IIS in the past. Ken Yes I am using windows and IIS. Is that an inherant problem? Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted February 21, 2007 Share Posted February 21, 2007 Hi I am having similar problems in this Post http://www.phpfreaks.com/forums/index.php/topic,128033.0.html here. I am using Win 2K SP4 IIS 5 I believe, although it seems to access data stored in a session from another page but clears data just omitted. Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 21, 2007 Share Posted February 21, 2007 By the way, the windows servers often have a default setting in PHP that needs to be changed: Try commenting this out in the php.ini session.use_only_cookies = 1 So that line should be: ; session.use_only_cookies = 1 Restart your server then try it. Quote Link to comment Share on other sites More sharing options...
timbrown Posted February 21, 2007 Author Share Posted February 21, 2007 Try using single quotes as well as using print_r to see if session data is there. page 1 <?php session_start(); $_SESSION['name'] = "tim"; print_r($_SESSION); ?> <html> <a href="page2.php">go[/url] </html> page 2 <?php session_start(); echo $_SESSION['name']; print_r($_SESSION); ?> Hi, page 1 outputs Array ( [name] => tim ) page 2 outputs Array () Quote Link to comment Share on other sites More sharing options...
timbrown Posted February 21, 2007 Author Share Posted February 21, 2007 By the way, the windows servers often have a default setting in PHP that needs to be changed: Try commenting this out in the php.ini session.use_only_cookies = 1 So that line should be: ; session.use_only_cookies = 1 Restart your server then try it. I checked and this line was already commented out. Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 21, 2007 Share Posted February 21, 2007 I'm going to reuse a post I posted before ----- Create a page with this code: Code: <?php session_start(); if (isset($_SESSION['count'])) { $_SESSION['count']++; } else { $_SESSION['count']=0; } echo $_SESSION['count']; ?> Then, go to that page. Refresh it a few times. See if the number it outputs increases. If it does increase, there is a problem with your script, if it doesn't, there is most likely a problem with your PHP session temp dir or a setting in php.ini Quote Link to comment Share on other sites More sharing options...
timbrown Posted February 21, 2007 Author Share Posted February 21, 2007 I'm going to reuse a post I posted before ----- Create a page with this code: Code: <?php session_start(); if (isset($_SESSION['count'])) { $_SESSION['count']++; } else { $_SESSION['count']=0; } echo $_SESSION['count']; ?> Then, go to that page. Refresh it a few times. See if the number it outputs increases. If it does increase, there is a problem with your script, if it doesn't, there is most likely a problem with your PHP session temp dir or a setting in php.ini Thanks for that post. the number just stays at 0 when I refresh. my php.ini file refers to session.save_path as c:\php\sessiondata, and that has all persmissions set on it. any other php.ini danger spots? Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted February 21, 2007 Share Posted February 21, 2007 Hi Tom, I tried your script and it did not increment. These are my session settings : session.save_handler = files session.save_path = "C:/php/tmp" session.use_cookies = false session.name = PHPSESSID session.auto_start = 0 session.cookie_lifetime = 20 session.cookie_path = / session.cookie_domain = session.serialize_handler = php session.gc_probability = 10 session.gc_divisor = 1000 session.gc_maxlifetime = 9000 session.bug_compat_42 = 0 session.bug_compat_warn = 1 session.referer_check = session.entropy_file = session.cache_limiter = nocache session.cache_expire = 180 session.use_trans_sid = 0 session.hash_function = 0 session.hash_bits_per_character = 5 url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry" Any ideas, where I have gone wrong? Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 21, 2007 Share Posted February 21, 2007 I am using IE, and the post one above looks very ugly to me, who agrees? Ted Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 21, 2007 Share Posted February 21, 2007 Ok, here's some things I see: PHP is saving your temp sessions to the directory below, not where you previously posted: C:/php/tmp Make sure that folder exists. Also, you can try setting use_cookies to 1 in your PHP.ini Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted February 21, 2007 Share Posted February 21, 2007 scottybwoy is different from timbrown, sorry to hijack the thread, but I am suffering the same problem. And why is it ugly, I just selected the settings I have set, cos I'm sure everyone has the same comments in their php.ini file. And isn't it down to the provider how they want their sessions handled. But I can see that mine isn't working how I want it too, any pointers would be helpful. Quote Link to comment Share on other sites More sharing options...
timbrown Posted February 21, 2007 Author Share Posted February 21, 2007 this is my (timbrown's) session settings... session.auto_start Off session.bug_compat_42 On session.bug_compat_warn On session.cache_expire 180 session.cache_limiter nocache session.cookie_domain no value session.cookie_lifetime 0 session.cookie_path /windows/temp session.cookie_secure Off session.entropy_file no value session.entropy_length 0 session.gc_divisor 100 session.gc_maxlifetime 1440 session.gc_probability 1 session.hash_bits_per_character 4 session.hash_function 0 session.name PHPSESSID session.referer_check no value session.save_handler files session.save_path C:\PHP\sessiondata session.serialize_handler php session.use_cookies On session.use_only_cookies Off session.use_trans_sid 0 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 21, 2007 Share Posted February 21, 2007 Should your cookie_path be in the same format as your save_path? Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 21, 2007 Share Posted February 21, 2007 I have to get back to work cause i keep spending to much time on these boards... So I am going to post my session variables. This is working on my system, you can try to mix and match and see what might be causing it. session.auto_start Off Off session.bug_compat_42 Off Off session.bug_compat_warn On On session.cache_expire 180 180 session.cache_limiter nocache nocache session.cookie_domain no value no value session.cookie_httponly Off Off session.cookie_lifetime 0 0 session.cookie_path / / session.cookie_secure Off Off session.entropy_file no value no value session.entropy_length 0 0 session.gc_divisor 1000 1000 session.gc_maxlifetime 1440 1440 session.gc_probability 1 1 session.hash_bits_per_character 5 5 session.hash_function 0 0 session.name PHPSESSID PHPSESSID session.referer_check no value no value session.save_handler files files session.save_path C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\php\upload C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\php\upload session.serialize_handler php php session.use_cookies On On session.use_only_cookies Off Off session.use_trans_sid 0 0 Quote Link to comment Share on other sites More sharing options...
timbrown Posted February 21, 2007 Author Share Posted February 21, 2007 I have to get back to work cause i keep spending to much time on these boards... So I am going to post my session variables. This is working on my system, you can try to mix and match and see what might be causing it. session.auto_start Off Off session.bug_compat_42 Off Off session.bug_compat_warn On On session.cache_expire 180 180 session.cache_limiter nocache nocache session.cookie_domain no value no value session.cookie_httponly Off Off session.cookie_lifetime 0 0 session.cookie_path / / session.cookie_secure Off Off session.entropy_file no value no value session.entropy_length 0 0 session.gc_divisor 1000 1000 session.gc_maxlifetime 1440 1440 session.gc_probability 1 1 session.hash_bits_per_character 5 5 session.hash_function 0 0 session.name PHPSESSID PHPSESSID session.referer_check no value no value session.save_handler files files session.save_path C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\php\upload C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\php\upload session.serialize_handler php php session.use_cookies On On session.use_only_cookies Off Off session.use_trans_sid 0 0 hey tom thanks so much. It works now!!!!!!! I just changed my cookie_path to / which is the default anyway. Hope you haven't jeopardised you paid emplyment helping me! Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted February 21, 2007 Share Posted February 21, 2007 Thanks to all, got mine fixed too now thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.