Simple incrementing counter not incrementing

I am trying to create a flag that when it reaches a number a message is displayed based on the value in a if statement. However, the flag value is not being incremented so the message based on the else statement is not being displayed.

I would be grateful if someone could point out my error. Thanks

          case "PASSWORD":

          $flag = 0;
          
          if($flag != 3) {
          echo "Supplied password is incorrect.<br />Please supply a valid password to proceed.";
          $flag++;
          } else {
          echo "flag limit has been reached.";
          }

How it could be incremented, if you set it any time to 0? Flag should be initialized with 0 only once and out of your switch and of this function completelly.

Sorry igor. Don’t understand your reply.Could you do example based on my code. What I am trying to do is if $flag !=3 then display message. But if $flag ==3 then display else message. Thanks

Correct…

class MyClass
{
    private $flag = 0;

    public function check($mode)
    {
        switch ($mode) {
            case 'PASSWORD':
                if ($this->flag <= 3) {
                    echo "Supplied password is incorrect.<br />Please supply a valid password to proceed.";
                    $this->flag += 1;
                } else {
                    echo "flag limit has been reached.";
                    $this->flag = 0; //could be
                }
                break;
         }      
     }
}

Thanks very much igor. But instead of class is there not a simpler way to just increment a variable. Thanks

I just would to say, this two lines…

          $flag = 0;
          
          if($flag != 3) {

mean that condition on second line will be always true.

Ok thanks

If you’re going to count how many times someone submits an incorrect password, you’ll have to deal with the page being reloaded, unless you’re using Ajax to submit the password. Maybe a cookie or session variable would help here, though it’s hard to be sure without knowing more about the scenario.

Thanks for input droopsnoot

Well, no. If the OP implements PRG (POST GET REDIRECT) page reloading is not an issue.

Unless the value is stored in the session it starts at 0 every page reload, basically rendering the entire check useless.

I assumed the count would be stored in a DB.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.