Jump to content

Need help for php login function

ashraf97

I have made a php login program and I was asked by my teacher to try and make a login disabler that displays after 3 unsuccessful attempts to try again later?

Link to comment
Share on other sites

Link to post
Share on other sites

Assuming we can forego any 'best practices' for security...simply set a session variable for the number of attempted logins.

 

For example...

 

Set or increment the session variable if login fails:

// If statement to check if log in failsif ($login->fails()){    $_SESSION['login_attempts'] = isset($_SESSION['login_attempts']) ? $_SESSION['login_attempts'] + 1 : 1;    // We will expire the session ourselves...    $_SESSION['last_login_attempt'] = time();        // Whatever else you need to do when authentication fails}

Then check the session variables before attempting to authenticate users:

if (!isset($_SESSION['login_attempts']) || isset($_SESSION['login_attempts']) &&    $_SESSION['login_attempts'] < 3 || isset($_SESSION['login_attempts']) && $_SESSION['login_attempts'] == 3 && time() - $_SESSION['last_login_attempt'] > 1800) // so much for DRY...{    // good}else{    // bad}

That would be a very simple way to do this.

Link to comment
Share on other sites

Link to post
Share on other sites

@Aho

 

haha thanks but is there anything simpler or anything that correlates with this?

 <?php   $name = $_POST['number1'];  $pass = $_POST['number2'];     if(strcmp($pass,'password') == 0 && strcmp($name,'joseph.foley') == 0)  {    echo 'Access Granted ';  }  else  {    echo 'Incorrect, please enter a valid user-name/password';  }?> 
Link to comment
Share on other sites

Link to post
Share on other sites

 

@Aho

 

haha thanks but is there anything simpler or anything that correlates with this?

 <?php   $name = $_POST['number1'];  $pass = $_POST['number2'];     if(strcmp($pass,'password') == 0 && strcmp($name,'joseph.foley') == 0)  {    echo 'Access Granted ';  }  else  {    echo 'Incorrect, please enter a valid user-name/password';  }?> 
 <?php   $name = $_POST['number1'];  $pass = $_POST['number2'];     if(strcmp($pass,'password') == 0 && strcmp($name,'joseph.foley') == 0 && ( !isset($_SESSION['login_attempts']) || $_SESSION['login_attempts'] < 3 ) )  {    echo 'Access Granted ';  }  else  {    if (!isset($_SESSION['login_attempts']))    {      echo 'Incorrect, please enter a valid user-name/password';    }    else if ($_SESSION['login_attempts'] >= 3)    {      echo 'Too many attempts, please try again later.';    }    if (isset($_SESSION['login_attempts']))    {       $_SESSION['login_attempts']++;    }    else    {       $_SESSION['login_attempts'] = 1;    }  }?> 
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×