Increment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adefunke
Forum Newbie
Posts: 2
Joined: Tue Oct 09, 2007 3:42 pm

Increment

Post 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?
markg85
Forum Commoner
Posts: 32
Joined: Sat Dec 03, 2005 6:49 pm

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
adefunke
Forum Newbie
Posts: 2
Joined: Tue Oct 09, 2007 3:42 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Also of note: postfixed ++ returns the original value before updating it. The assignment shouldn't be there either.
Post Reply