naddie Posted February 24, 2010 Share Posted February 24, 2010 Hi, I've searched as much as I could and couldn't find an answer to this. I'm currently using the latest version of WAMP Server (2.0i). I'm trying to figure out how to use $_SESSION variables, and either passing them to another page, or using them on the same page again after a refresh. With this simple example from a tutorial I read: <?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1; ?> With this code, the counter should update on a page refresh. But it stays at 1. session_start() is there, and I get no errors when I refresh the page or send it to another page. Is there something I'm missing or does something need to be configured in WAMP for it to work? Any help would be appreciated, thanks! Quote Link to comment Share on other sites More sharing options...
juanpablo Posted February 24, 2010 Share Posted February 24, 2010 Hi, I've searched as much as I could and couldn't find an answer to this. I'm currently using the latest version of WAMP Server (2.0i). I'm trying to figure out how to use $_SESSION variables, and either passing them to another page, or using them on the same page again after a refresh. With this simple example from a tutorial I read: <?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1; ?> With this code, the counter should update on a page refresh. But it stays at 1. session_start() is there, and I get no errors when I refresh the page or send it to another page. Is there something I'm missing or does something need to be configured in WAMP for it to work? Any help would be appreciated, thanks! Try running this code, if you get the "setting it to 1" message more than once... well, i dont know lol. <?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1; echo "variable wasnt set... setting it to 1"; ?> Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 I've tried an echo test like you described, and that msg is always displayed, which means the isset is failing, obviously. I'm just not sure why its not setting, since the code looks correct as far as I know? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 24, 2010 Share Posted February 24, 2010 I get no errors Do you have error_reporting set to E_ALL (or higher) and display_errors set to ON in your master php.ini and you have confirmed what the settings actually are using a phpinfo(); statement (in case the php.ini that you are changing is not the one that php is using)? Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 Yes, error_reporting is set to E_ALL, and display_errors is also set to ON. Running phpinfo() confirms display_errors is ON, but I'm not sure about error_reporting, it lists "30719". Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 <?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1; echo "variable wasnt set... setting it to 1"; ?> You have no brackets on your if. You can go without brackets but only the first statement after will be process as part of the if. Thats why you always see the echo in the else cause without brackets its by default not part of the if /else statement. When you have problems why not try echo out the var in question. You will see that it probably does increment. HTH Teamtomic Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 That was juanpablo's suggestion above. I used the brackets when testing which part of the if/else is true. After the if/else, I echo'd the variable by itself which is how I know its not incrementing. I just says "1". When I try to send the variable to another page it says "undefined index ... etc" I know said I had no errors in my original post, but I misspoke, I guess meant no errors when refreshing the page. I DO get an error when sending it another page, showing that it's not set. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 <?php session_start(); if(isset($_SESSION['views'])) { $_SESSION['views'] = $_SESSION['views']+ 1; echo "$_SESSION['views']; } else { $_SESSION['views'] = 1; echo $_SESSION['views'];; } ?> Put this in. cut and paste it. Refresh the page a few times. what happens? HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 The output stays at "1" even after multiple refreshes. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 Check the rest of the page. Do you somewhere on it maybe reset it? Cause what you are describing should not be happening. If its set to one it should be passing the isset and increment. If you do not reset the var somewhere then your sessions are not working. What does it say about sessions with a phpinfo()? Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 All that's in the code right now is what you posted for me. And the "1" is the only output. I don't really understand the session info from phpinfo().. I'll paste it here: Session Support enabled Registered save handlers files user Registered serializer handlers php php_binary wddx Directive Local Value Master Value session.auto_start Off Off session.bug_compat_42 On On 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:/wamp/tmp c:/wamp/tmp session.serialize_handler php php session.use_cookies On On session.use_only_cookies On On session.use_trans_sid 0 0 (apologies, not sure how to put it into a code box or anything) I thought maybe something is messed up in the settings, since the code looks right.. but I'm not sure what should be enabled etc. Thanks for the help, btw! Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted February 24, 2010 Share Posted February 24, 2010 just a test real quick: try this: <?php session_start(); $_SESSION['test'] = "TESTING 1 2 3"; echo $_SESSION['test']; ?> Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 That code outputs "TESTING 1 2 3". Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 The output from phpinfo is fine, your sessions should work as expected. Does c:/wamp/tmp exist and are session files going into it? Show the complete page that you are initializing the session on. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 I'm not sure what you mean by complete page? If you mean the code, right now it's simply the example I gave. c:/wamp/tmp does exist and there are several session files in there. I opened a few up, and they all say 'views' has a value of 1. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 24, 2010 Share Posted February 24, 2010 What is your actual code that is displaying the value? You have only posted your code that is testing if it is set and incrementing it or setting it to 1. Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 <?php session_start(); if(isset($_SESSION['views'])) { $_SESSION['views'] = $_SESSION['views']+ 1; echo $_SESSION['views']; } else { $_SESSION['views'] = 1; echo $_SESSION['views']; } ?> This is the current code from a previous poster (I don't know how to put it in a code box). It's just a simple example to see if sessions are working. The echo $_SESSION['views']; is what is displaying the value. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 24, 2010 Share Posted February 24, 2010 Add the following three lines of code immediately after the first opening <?php tag - ini_set("display_startup_errors", "1"); // I suspect this setting will reveal the cause of the problem ini_set("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 Okay, I added those, so this is the current code: <?php ini_set("display_startup_errors", "1"); // I suspect this setting will reveal the cause of the problem ini_set("display_errors", "1"); error_reporting(E_ALL); session_start(); if(isset($_SESSION['views'])) { $_SESSION['views'] = $_SESSION['views']+ 1; echo $_SESSION['views']; } else { $_SESSION['views'] = 1; echo $_SESSION['views']; } ?> And it's still displaying just the '1' after multiple refreshes. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 24, 2010 Share Posted February 24, 2010 Any chance you have configured your browser so that it does not accept any cookies. Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 err, yes, I have it set to not allow cookies. I thought sessions worked even cookies are disabled? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 24, 2010 Share Posted February 24, 2010 The session id is propagated between pages by the browser. The default method (and per your phpinfo() output) is to use a session id cookie. Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 Okay, silly me. I added an exception for localhost, and it's now working. Thank you! I apologize, I should have tried that. I really thought they were different. One last question if you don't mind, and I'll stop bugging you guys. Is it possible to use session variables without cookies? If so, what settings need to be changed? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 24, 2010 Share Posted February 24, 2010 http://us2.php.net/manual/en/session.idpassing.php Quote Link to comment Share on other sites More sharing options...
naddie Posted February 24, 2010 Author Share Posted February 24, 2010 Okay, thanks everyone for all their help! 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.