Password Entry Counter

Ask about a PHP problem here.
Post Reply
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Password Entry Counter

Post by wrichards8 »

I didn't know where I ought to post this. I am trying to write a script which will prevent access to a users' account for 20 minutes after 4 failed login attempts. I am attempting to implement this as a function so I can call it on the login and activation [age. The code I have so far is:
function sideaccess($key, $mode)
	{
		global $db_host;
		global $db_user; 
		global $db_pass;
		global $db_base;
		$output = "";
		mysql_connect($db_host, $db_user, $db_pass);
		mysql_select_db($db_base);
		$select = mysql_query("SELECT `accessid`,`userid`,`timestring`,`attempts` FROM accesslog WHERE userid='$key'");
		$timestring = strtotime(date("d/m/Y. H:i:s", strtotime("+20 minutes")));
		If ($mode==1)
		{
			$rows = mysql_num_rows($select);		
			if($rows==0)
			{
				$output == FALSE;
			}
			else
			{
				$read = mysql_fetch_assoc($select);
				$attempts = $read["attempts"];
				$chectime = $read["timestring"];
			}
		}
		elseif ($mode==2)
		{
			$sql = "INSERT INTO accesslog (`userid`,`timestring`,`attempts`) VALUES ('$key','$timestring') ON DUPLICATE KEY UPDATE `attempts` =  'attempts' + 1;";			
			$insert = mysql_query($sql); 
			if(!$insert)
			{
				echo mysql_error();
			}
		}
		else
		{
			$output = "Function number ".$mode." not valid";
		}
		return $output;
	}
The key is the user id, and the mode is either 1 or 2 depending on whether you want to write to the database. SQL database currently looks like this:
CREATE TABLE `accesslog`
	(
		`accessid` INT(6) NOT NULL PRIMARY KEY AUTO_INCREMENT,
		`userid` INT(6) NOT NULL,
		`timestring` INT(10) NOT NULL,
		`attempts` INT(4) NOT NULL 
	)	
ENGINE = InnoDB;	

ALTER TABLE `accesslog` ADD INDEX (`userid`); ALTER TABLE `accesslog` ADD FOREIGN KEY (`userid`) REFERENCES `useraccounts` (`userid`) ON DELETE CASCADE ON UPDATE CASCADE;
I have been trying for a couple of days and it hasn't worked. This is the code I have so far from trying to rewrite this function. Please, someone, help?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Password Entry Counter

Post by jacek »

You need to track the number of attempts per IP really, if I tried to login to your account 5 times from my PC here it would block me. But if you happened to try and log in during the 20 minutes it would block you which would be pretty irritating.

I don't really get your problem though, can you explain what is actually wrong ?
Image
Post Reply