doubledee Posted September 2, 2011 Share Posted September 2, 2011 I am trying to keep track of how many times a page is loaded for debugging. Here is my code but it doesn't seem to work?! <?php isset($counter) ? $counter++ : $counter=1; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Page A</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <h3>Add a Comment</h3> <form action="Processing.php" method="post"> <fieldset> <!-- Comment --> <label>Comment:</label><br /> <textarea cols="50" rows="15"><?php echo $counter; ?></textarea> <br /> <!-- Submit Form --> <input type="submit" name="addComment" id="addComment" value="Add a Comment"/> </fieldset> </form> </body> </html> What am I doing wrong? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246293-incrementing-counter/ Share on other sites More sharing options...
monkeytooth Posted September 2, 2011 Share Posted September 2, 2011 <?php isset($counter) ? $counter++ : $counter=1; ?> the idea is right.. However, php doesn't store variables past the pages load. You will have to make use of sessions or cookies to achieve this. Quote Link to comment https://forums.phpfreaks.com/topic/246293-incrementing-counter/#findComment-1264828 Share on other sites More sharing options...
jamesxg1 Posted September 2, 2011 Share Posted September 2, 2011 <?php session_start(); if(isset($_SESSION['counter'])) { $_SESSION['counter']++; } else { $_SESSION['counter'] = 1; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Page A</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <h3>Add a Comment</h3> <form action="Processing.php" method="post"> <fieldset> <!-- Comment --> <label>Comment:</label><br /> <textarea cols="50" rows="15"><?php echo $_SESSION['counter']; ?></textarea> <br /> <!-- Submit Form --> <input type="submit" name="addComment" id="addComment" value="Add a Comment"/> </fieldset> </form> </body> </html> James. Quote Link to comment https://forums.phpfreaks.com/topic/246293-incrementing-counter/#findComment-1264832 Share on other sites More sharing options...
doubledee Posted September 2, 2011 Author Share Posted September 2, 2011 <?php isset($counter) ? $counter++ : $counter=1; ?> the idea is right.. However, php doesn't store variables past the pages load. You will have to make use of sessions or cookies to achieve this. My brain isn't working today. How would I use a Session with this? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246293-incrementing-counter/#findComment-1264835 Share on other sites More sharing options...
monkeytooth Posted September 2, 2011 Share Posted September 2, 2011 no worries my brains the same way today.. but fortunately for both of us.. jamesxg1 answered it pretty well Quote Link to comment https://forums.phpfreaks.com/topic/246293-incrementing-counter/#findComment-1264836 Share on other sites More sharing options...
doubledee Posted September 2, 2011 Author Share Posted September 2, 2011 Why doesn't this work?? <?php session_start(); isset($_SESSION['$counter']) ? $_SESSION['$counter']++ : $_SESSION['$counter']=1; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Page A</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <h3>Add a Comment</h3> <form action="Processing.php" method="post"> <fieldset> <!-- Comment --> <label>Comment:</label><br /> <textarea cols="50" rows="15"><?php echo $_SESSION['counter']; ?></textarea> <br /> <!-- Submit Form --> <input type="submit" name="addComment" id="addComment" value="Add a Comment"/> </fieldset> </form> </body> </html> Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246293-incrementing-counter/#findComment-1264839 Share on other sites More sharing options...
KevinM1 Posted September 2, 2011 Share Posted September 2, 2011 $_SESSION['$counter'] is not the same as $_SESSION['counter'] Quote Link to comment https://forums.phpfreaks.com/topic/246293-incrementing-counter/#findComment-1264841 Share on other sites More sharing options...
silkfire Posted September 2, 2011 Share Posted September 2, 2011 "My brain isn't working today." You're a bit right there, matey =) Don't use $ in the array key name. $_SESSION['$counter'] should be $_SESSION['counter']. Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/246293-incrementing-counter/#findComment-1264842 Share on other sites More sharing options...
doubledee Posted September 2, 2011 Author Share Posted September 2, 2011 $_SESSION['$counter'] is not the same as $_SESSION['counter'] Told ya my brain was malfunctioning! (Didn't get to bed until about 4am...) Thanks! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246293-incrementing-counter/#findComment-1264846 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.