Walker Posted November 6, 2009 Share Posted November 6, 2009 Hi, I'm semi-new to PHP. I can't get sessions to work, and I'm starting to think that my professor has something set up wrong in the php.ini file. I've looked all over the web, done half a dozen tutorials, and searched through a 50 different forums, and it still doesn't work. Finally, I copied and pasted some code from the php.net documentation and it still won't work. Here's the code: --------------------- <?php session_start(); if (empty($_SESSION['count'])) { $_SESSION['count'] = 1; } else { $_SESSION['count']++; } ?> <p> Hello visitor, you have seen this page <?php echo $_SESSION['count']; ?> times. </p> <p> To continue, <a href="nextpage.php?<?php echo htmlspecialchars(SID); ?>">click here</a>. </p> --------------------- Obviously, this is supposed to increment the counter by one each time the page loads. But, it doesn't work. Each time you reload the page from the address bar, or by refreshing, or by clicking the self-referencing link, the counter displays "1". For proof, go to rebel.slcc.edu/~joshuaw/fish/nextpage.php WHAT IS THE PROBLEM? I see three possibilities: 1) I'm retarted. It's possible that I've been staring at code so long that I'm missing something obvious. 2) The PHP settings are wrong somewhere. I can provide those if needed. 3) There are local settings one my machine that are preventing the proper use of the session. My understanding of sessions tells me that this is the least likely option because I've tested it on three different computers on two different networks. Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/180561-help-with-sessions/ Share on other sites More sharing options...
jonsjava Posted November 6, 2009 Share Posted November 6, 2009 Your script never checked to see if $_SESSION['count'] was set, only if it was empty: <?php session_start(); if (!isset($_SESSION['count']) || empty($_SESSION['count'])){ $_SESSION['count'] = 1; } else { $_SESSION['count']++; } ?> <p> Hello visitor, you have seen this page <?php echo $_SESSION['count']; ?> times. </p> <p> To continue, <a href="nextpage.php?<?php echo htmlspecialchars(SID); ?>">click here</a>. </p> Quote Link to comment https://forums.phpfreaks.com/topic/180561-help-with-sessions/#findComment-952583 Share on other sites More sharing options...
PFMaBiSmAd Posted November 6, 2009 Share Posted November 6, 2009 The first posted code does in fact work (assuming sessions are configured correctly on the server at all and that your file has not been saved in a format where something is being output to the browser that would prevent the session from working.) Are you learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini to get php to display all the errors it detects? If there is a problem with how sessions are configured on the server or a problem with something about your file format that prevents sessions from working, there would be php errors that would help pinpoint the problem. Quote Link to comment https://forums.phpfreaks.com/topic/180561-help-with-sessions/#findComment-952629 Share on other sites More sharing options...
mikesta707 Posted November 6, 2009 Share Posted November 6, 2009 Just tested your code and it works perfectly on my server Quote Link to comment https://forums.phpfreaks.com/topic/180561-help-with-sessions/#findComment-952633 Share on other sites More sharing options...
Walker Posted November 6, 2009 Author Share Posted November 6, 2009 Okay, so I added these lines to the code: ----------------------------------- error_reporting(E_ALL); ini_set('display_errors','On'); ----------------------------------- and got these error messages: ----------------------------------- Warning: Unknown: open(/tmp//sess_f77cpui9u8lidn506k97qu8540, O_RDWR) failed: No such file or directory (2) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct () in Unknown on line 0 ----------------------------------- Then I added this line to my code: ----------------------------------- session_save_path('5;/tmp'); ----------------------------------- and the error messages changed to: ----------------------------------- Warning: Unknown: open(/tmp/f/7/7/c/p/sess_f77cpui9u8lidn506k97qu8540, O_RDWR) failed: No such file or directory (2) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (5;/tmp) in Unknown on line 0 ----------------------------------- Again, I searched several boards for clues regarding these errors, but to no avail. (and, yes, I googled them as well) Do these errors shed any light on the problem? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/180561-help-with-sessions/#findComment-952660 Share on other sites More sharing options...
PFMaBiSmAd Posted November 7, 2009 Share Posted November 7, 2009 session.save_path must point to a folder that actually exists. Quote Link to comment https://forums.phpfreaks.com/topic/180561-help-with-sessions/#findComment-953131 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.