Page 1 of 1

Increment

Posted: Sat Oct 13, 2007 9:25 am
by adefunke
Maybe I'm just being silly, but the more I've been working on this, the more I mess it up, and now it doesn't even echo counter so I can't see tell where the problem is anymore... HELP!!! :cry:

If tried to add a condition to a web page that counts how many times someone tries to log in. I want the value of counter to go up only when I reload the page. When I set the value of $counter to 0, it comes back with 0. If I have it go through a different page it comes back as one, but that would mean going through a different page each time.

I'm so confused with it all :?

Code: Select all

<?php
$counter = $counter++;
echo $counter;
if ($counter == 0) {
	echo $form;} 
elseif (($counter > 0) && ($counter < 5)) {
	echo "<p>Sorry but that username and password combination is not valid. Please try again.</p>";
	echo $form;}
else {
	echo "<p>Sorry, you have exceeded the allocated number of attempts for authorisation. Please contact the <a href=\"admin.php\">administrator</a>.</p>";
	} 
?>
Can someone please tell me where I'm going wrong?

Posted: Sat Oct 13, 2007 10:17 am
by markg85
Well it's not working for a few reasons.

1. You never give $counter a value so therefore it can't be incremented.
2. And what you try to do in the php will never work because $counter doesn't have a number at all.

good luck.

Posted: Sat Oct 13, 2007 10:59 am
by superdezign
$counter will be instantiated to a null value every time you load the page. Page requests are completely independent of one another and values do not persist from one page request to the next. If you want to store the amount of login tries, you may want to use sessions.

Posted: Sat Oct 13, 2007 1:52 pm
by adefunke
Thank you superdezign, I'll try that. In case you hadn't guessed I'm kind of new to this.

edit: Just tried it. Worked a treat. Thanks superdezign!!! :D

Posted: Sun Oct 14, 2007 6:38 am
by feyd
Also of note: postfixed ++ returns the original value before updating it. The assignment shouldn't be there either.