Simple login page with only 3 password attempts

Posted 18 July 2012 - 08:12 AM

I don't know why but I can't finish my activity on my PHP class on a simple program. Just a login page with only 3 attempts on password-entry. I already got the logic which is pretty easy, if successful login, break the loop, otherwise, continue with the loop with a counter up to 3.

But the problem is, I'm not sure how to form the loop. I tried looping the entire if-else block, but the if statements seem to not work and the text output is displaying 3 times.
Also, I know that some methods besides using loop will be easier, but we're asked to make a simple loop statement since we haven't discussed beyond that topic yet.

Replies To: Simple login page with only 3 password attempts

Re: Simple login page with only 3 password attempts

Posted 18 July 2012 - 08:20 AM

I really don't see how it's ever going to work with a while loop. PHP will execute the loop three times each page load, the HTML output is then rendered and displayed to the user, the PHP is not reprocessed anytime until the page is reloaded. PHP does not execute in real-time during user interaction.

What you want to be doing is using Sessions to store how many attempts the user has had. Incrementing this each time they fail to login. If the session value is then 3, don't attempt to login.

Re: Simple login page with only 3 password attempts

Posted 18 July 2012 - 08:21 AM

But our professor said it's just a simple loop. Can be any type of loop. I tried the for loop and it displays the message three times. And it seems like it doesn't increment the counter up to 3 and recognized the if statement that if the loop comes to 3, it will stop.
The session thing isn't discussed yet so im not sure if i can do that.

Re: Simple login page with only 3 password attempts

Posted 18 July 2012 - 09:00 AM

As I said in my previous message, the loop will run three times when the page LOADS and only when it loads. It will not wait for them to attempt to log-in again. That's not how PHP runs. As far as I see it you cannot be using a PHP loop for login attempts, it's just not going to work.

If your professor has actually asked you to specifically use a PHP loop for login attempts then I suggest having some words with him.

Re: Simple login page with only 3 password attempts

Posted 18 July 2012 - 09:05 AM

So that's why the message "Try again" is displaying as "Try again Try again Try again"?
Well he only said that the program uses loop and he gave us the logic. So the $_Session is the only way? So I'll just add $_SESSION to my counter (which is $ctr), and the loop structure is just the same? I'll just use the code you gave and modify it.

Re: Simple login page with only 3 password attempts

Posted 18 July 2012 - 09:05 AM

So that's why the message "Try again" is displaying as "Try again Try again Try again"?
Well he only said that the program uses loop and he gave us the logic. So the $_Session is the only way? So I'll just add $_SESSION to my counter (which is $ctr), and the loop structure is just the same? I'll just use the code you gave and modify it.

Re: Simple login page with only 3 password attempts

Posted 18 July 2012 - 09:05 AM

So that's why the message "Try again" is displaying as "Try again Try again Try again"?
Well he only said that the program uses loop and he gave us the logic. So the $_Session is the only way? So I'll just add $_SESSION to my counter (which is $ctr), and the loop structure is just the same? I'll just use the code you gave and modify it.

Re: Simple login page with only 3 password attempts

Posted 18 July 2012 - 10:42 AM

The reason you're getting three outputs is because the loop is running three times before you break it. As I said, PHP is a pre-processor, it will not compute in real-time. Don't throw the $_SESSION code into yours and expect it to work, read up on how sessions work in the PHP manual.