jbooth952 Posted September 2, 2008 Share Posted September 2, 2008 I mostly copied the code below from samples in tutorials, but it won't work for me. The "if (isset)" always takes the "else" path, and the page linked to, never finds the counter. Do I have to set other parms? Do I have to change the php.ini? Help! <?php session_start(); if (isset( $_SESSION['counter'])) { $_SESSION['counter'] = $_SESSION['counter'] + 1; print "at the if " . $_SESSION['counter']; } else { $_SESSION['counter'] = 1; print "at the else " . $_SESSION['counter']; } $msg = "You have visited this page ". $_SESSION['counter']; $msg .= " times in this session."; ?> <html> <head> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="keywords" content="HTML, XHTML, JavaScript" /> <title>Setting up a PHP session</title> </head> <body> <?php echo ( $msg ); ?> <p> To continue click following link <br /> <a href="newpage.html">different page</a> !-- </p> </body> </html> Quote Link to comment Share on other sites More sharing options...
revraz Posted September 2, 2008 Share Posted September 2, 2008 So are you refreshing this page to see if the counter works? You can check the php.ini to make sure the session save path is correct. Quote Link to comment Share on other sites More sharing options...
webxan Posted September 2, 2008 Share Posted September 2, 2008 try changing the session path in php.ini to some directory on your system where you want to store them, may be in some tmp dir Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 2, 2008 Share Posted September 2, 2008 Add the following two lines immediately after your <?php tag (move the session_start(); on that line down) - ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment Share on other sites More sharing options...
revraz Posted September 2, 2008 Share Posted September 2, 2008 Good idea, wonder why no one else said that. try changing the session path in php.ini to some directory on your system where you want to store them, may be in some tmp dir Quote Link to comment Share on other sites More sharing options...
jbooth952 Posted September 2, 2008 Author Share Posted September 2, 2008 try changing the session path in php.ini to some directory on your system where you want to store them, may be in some tmp dir In searching my web host's knowledge base, I found that I need to change the path where the session info is stored. They gave me the real path name, but they didn't tell me how to code the script so that I correctly point there. What commands change/set the system to point to that directory? And, do they go before or after session_start() ? When you're a customer on a webserver host, can you change the php.ini itself? I've been a programmer for many years, but I'm new to client-server and PHP, so sorry for the dumb questions. Thank you. Quote Link to comment Share on other sites More sharing options...
revraz Posted September 2, 2008 Share Posted September 2, 2008 You don't change anything in your code, you change the php.ini file to that path. Quote Link to comment Share on other sites More sharing options...
jbooth952 Posted September 2, 2008 Author Share Posted September 2, 2008 You don't change anything in your code, you change the php.ini file to that path. I'm on a hosting service. Does each account have its own php.ini? Can I change it? Then, I guess if I can, I need to find out where it is, and how to change it on the host's server. Thanks, Jim Quote Link to comment Share on other sites More sharing options...
webxan Posted September 3, 2008 Share Posted September 3, 2008 well it depends on the how php is setup on the server. if you have .htaccess file in your root directory then use php_value session.save_path '/tmp' or if your hosting has provided you with php.ini file in your root dir then you might use session.save_path = "/tmp" or otherwise try to add this code of line before session_start() in your script ini_set('session.save_path','/tmp'); where /tmp is the directory path that you will have to change to whatever directory you have access to try changing the session path in php.ini to some directory on your system where you want to store them, may be in some tmp dir In searching my web host's knowledge base, I found that I need to change the path where the session info is stored. They gave me the real path name, but they didn't tell me how to code the script so that I correctly point there. What commands change/set the system to point to that directory? And, do they go before or after session_start() ? When you're a customer on a webserver host, can you change the php.ini itself? I've been a programmer for many years, but I'm new to client-server and PHP, so sorry for the dumb questions. Thank you. Quote Link to comment Share on other sites More sharing options...
jbooth952 Posted September 3, 2008 Author Share Posted September 3, 2008 I don't know if you want to bother with this, but if you're curious read on; If you go to this URL http://firstpickclub.com/session_test.php it works correctly. Each time you refresh, the counter increases. This page contains ONLY PHP code as follows with NO html; <?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "Views=". $_SESSION['views']; ?> Now, I copied that code into another file that you can browse; http://firstpickclub.com/v1sessiontest2.php It has the following code (the above code plus html), and it does not increase the count on refresh; UNLESS, while still in the browser, I go to the first URL that has no html, and refresh it once, and then go back to this page and click refresh twice. This is very strange. <?php session_save_path("the path they provided removed for security") it acts the same with or without this line, BTW session_start(); if(isset($_SESSION['views'])) { $_SESSION['views']=$_SESSION['views']+1; } else { $_SESSION['views']=1; } echo "Views=". $_SESSION['views']; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="keywords" content="HTML, XHTML, JavaScript" /> <title>Setting up a PHP session</title> </head> <body> <?php $msg = $_SESSION['views']; ?> <p> <?php echo "In body count = " . $msg; ?></p> <p> To continue click following link <br /> <a href="v1newpage.php">different page</a> </p> </body> </html> well it depends on the how php is setup on the server. if you have .htaccess file in your root directory then use php_value session.save_path '/tmp' or if your hosting has provided you with php.ini file in your root dir then you might use session.save_path = "/tmp" or otherwise try to add this code of line before session_start() in your script ini_set('session.save_path','/tmp'); where /tmp is the directory path that you will have to change to whatever directory you have access to try changing the session path in php.ini to some directory on your system where you want to store them, may be in some tmp dir In searching my web host's knowledge base, I found that I need to change the path where the session info is stored. They gave me the real path name, but they didn't tell me how to code the script so that I correctly point there. What commands change/set the system to point to that directory? And, do they go before or after session_start() ? When you're a customer on a webserver host, can you change the php.ini itself? I've been a programmer for many years, but I'm new to client-server and PHP, so sorry for the dumb questions. Thank you. Quote Link to comment Share on other sites More sharing options...
jbooth952 Posted September 5, 2008 Author Share Posted September 5, 2008 Unbelievable! The source code was saved from Notepad as UTF-8 and I had charset=UTF-8 in the HTML. I saved the source as ANSI and changed the charset to windows-1252 and now it works. Why is this the case? Is it because I use a Windows server? Quote Link to comment Share on other sites More sharing options...
dezkit Posted September 5, 2008 Share Posted September 5, 2008 http://firstpickclub.com/session_test.php seems to be working Quote Link to comment Share on other sites More sharing options...
revraz Posted September 5, 2008 Share Posted September 5, 2008 Means you were getting a Error that the session couldn't start because of output, and you have error display turned off. Unbelievable! The source code was saved from Notepad as UTF-8 and I had charset=UTF-8 in the HTML. I saved the source as ANSI and changed the charset to windows-1252 and now it works. Why is this the case? Is it because I use a Windows server? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 5, 2008 Share Posted September 5, 2008 You could have found that problem three days a go by following what this post stated - Add the following two lines immediately after your <?php tag (move the session_start(); on that line down) - ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment Share on other sites More sharing options...
jbooth952 Posted September 6, 2008 Author Share Posted September 6, 2008 http://firstpickclub.com/session_test.php seems to be working Yes, it is now, after I changed the character set type from UTF-8 to windows-1252 I stated that in a post above Quote Link to comment Share on other sites More sharing options...
jbooth952 Posted September 6, 2008 Author Share Posted September 6, 2008 You could have found that problem three days a go by following what this post stated - Add the following two lines immediately after your <?php tag (move the session_start(); on that line down) - ini_set ("display_errors", "1"); error_reporting(E_ALL); I'm new to PHP and didn't even know about that until 2 days ago, and I didn't think that had anything to do with character sets. I still don't understand why the character set should affect commands. Quote Link to comment Share on other sites More sharing options...
jbooth952 Posted September 6, 2008 Author Share Posted September 6, 2008 You could have found that problem three days a go by following what this post stated - Add the following two lines immediately after your <?php tag (move the session_start(); on that line down) - ini_set ("display_errors", "1"); error_reporting(E_ALL); I'm new to PHP and didn't even know about that until 2 days ago, and I didn't think that had anything to do with character sets. I still don't understand why the character set should affect commands. Now I'm really confused. I went back and copied the new, working pages that are in ANSI format, added the error display code above and saved them as UTF-8 to see what errors would show up, and now the utf-8 format pages work just fine, and no errors show. What the he!!? 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.