Jump to content

session variable incrementing


virken

Recommended Posts

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']++;?> ">
Link to comment
https://forums.phpfreaks.com/topic/31155-session-variable-incrementing/
Share on other sites

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.

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.php

For example, if the value of submit was "0" then [color=blue]if($_POST['submit'])[/color] would not return expected results.

HTH :P
[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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.