virken Posted December 18, 2006 Share Posted December 18, 2006 My app. uses a two-page design where the first page poses a question and the second page provides an answer and scoring. It uses hidden fields and POSTS to Session variables to increment counters for the number of questions taken and answered correctly. It works pretty well except when back navigating using the BACK button. This UI action causes the counters to increment and mess up the scoring.The problem appears to be the snippet below which increments the variable whenever the page is loaded. Is there a way to write this so that the variable only gets incremented when the FORM is submitted - and not each time the page gets loaded?<input name="total incr" type="hidden" value=" <?php $_SESSION['total']++;?> "> Quote Link to comment https://forums.phpfreaks.com/topic/31155-session-variable-incrementing/ Share on other sites More sharing options...
thepip3r Posted December 18, 2006 Share Posted December 18, 2006 yes... on your check to see if the button is clicked:[code]if ($_POST['submit']) { $_SESSION['total']++; ...other code on button submit}[/code]I'd also recommend using a little checking using something like HTTP_REFERER [url=http://www.phpfreaks.com/phpref/125.php]http://www.phpfreaks.com/phpref/125.php[/url] to ensure that the new page has been loaded before incrementing the variable so you don't get a double increment similar to thep roblem you're having now. Quote Link to comment https://forums.phpfreaks.com/topic/31155-session-variable-incrementing/#findComment-143950 Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 Sure...[code]<?phpif (isset($_POST['submit'])){ $_SESSION['total']++;}?><input name="total incr" type="hidden" value="<?php $_SESSION['total'];?>">[/code]RegardsHuggieEDIT: You're too fast for me pip3r :) Quote Link to comment https://forums.phpfreaks.com/topic/31155-session-variable-incrementing/#findComment-143952 Share on other sites More sharing options...
thepip3r Posted December 18, 2006 Share Posted December 18, 2006 haha... we're like fixing a pin-hole problem with a sledge-hammer. =P as long as it gets done though. Quote Link to comment https://forums.phpfreaks.com/topic/31155-session-variable-incrementing/#findComment-143955 Share on other sites More sharing options...
heckenschutze Posted December 18, 2006 Share Posted December 18, 2006 HuggieBear's method is infact better than thepip3r's, reason: The comparitive check made on $_POST['submit'];I suggest you read: http://php.net/manual/en/types.comparisons.phpFor example, if the value of submit was "0" then [color=blue]if($_POST['submit'])[/color] would not return expected results.HTH :P Quote Link to comment https://forums.phpfreaks.com/topic/31155-session-variable-incrementing/#findComment-143966 Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 [quote author=heckenschutze link=topic=119165.msg487689#msg487689 date=1166478628]if the value of submit was "0" then [color=blue]if($_POST['submit'])[/color] would not return expected results.[/quote]This actually came up in this forum today on one of the posts.Huggie Quote Link to comment https://forums.phpfreaks.com/topic/31155-session-variable-incrementing/#findComment-143970 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.