ciao, sono 2 gg che cerco di inserire un controllo nel login, ovvero ho impostato tutto ciò che riguarda registrazione, recupero password, autenticazione.
Come posso inserire il reset della password dopo 3 tentativi di login falliti?
Di seguito il mio codice, la user.class e il login.php
----parte dello user.class
<?php
function session_handler()
{
$ret = 0;
if (isset($_SESSION["user_acc"])) {
$this->username=$_SESSION["user_acc"];
$ret = 1;
}
if (isset($_SESSION["pass_acc"])) {
$this->password=$_SESSION["pass_acc"];
$ret = 1;
}
if (isset($_SESSION["id_acc"])) {
$this->id=$_SESSION["id_acc"];
$ret = 1;
}
if (isset($_SESSION["profilo"])) {
$this->profilo=$_SESSION["profilo"];
$ret = 1;
}
return($ret);
}
function post_handler()
{
$ret = 0;
if (isset($_POST["user_acc"])) {
$this->username=$_POST["user_acc"];
$ret = 1;
}
if (isset($_POST["pass_acc"])) {
$this->password=$_POST["pass_acc"];
$ret = 1;
}
return($ret);
}
function auth()
{
global $db;
$this->session_handler();
$this->post_handler();
$ida = 0;
$sql = "Select ".$this->pk." From ".$this->tabella." ";
$sql .= "Where username='".$this->username."' And password='".$this->password."'";
$db->query($sql);
$db->next_record();
$ida = $db->f($this->pk);
if ($ida > 0)
{
$this->id = $ida;
$_SESSION["user_acc"] = $this->username;
$_SESSION["pass_acc"] = $this->password;
$_SESSION["id_acc"] = $this->id;
$_SESSION["profilo"] = $this->profilo;
$this->User($this->id);
}
else
{
$this->id = 0;
$this->logout();
}
}
function creaPwd()
{
$lenght = 8;
$possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ*+% $&#";
$maxlength = strlen($possible);
if ($length > $maxlength) {
$length = $maxlength;
}
for ($i = 0; $i<$lenght; $i++)
{
//prende un carattere random tra quelli possibili
$char = substr($possible, mt_rand(0, $maxlength-1), 1);
//è stato già usato?
if (!strstr($password, $char)) { //no
$password .= $char;
}
}
$this->password = $password;
}
//username = codice fiscale
//password = random
function creaAccount()
{
global $db;
$this->creaPwd();
$sql = "UPDATE user ";
$sql .="SET username='".$this->codice_fiscale."', password='".$this->password."'";
$sql .= " WHERE id = '".$this->id."'";
$db->query($sql);
//invia mail con account
mail($this->email, "Nuovo utente", "Username:".$this->username."\nPassword:".$this->password);
mail(EMAIL, "Nuovo utente", "Creato nuovo agente: Username:".$this->username."\n");
}
function sendPwd($username, $email)
{
global $db;
$this->creaPwd();
$sql = "SELECT id, email, username FROM user ";
$sql .= " WHERE username = '".$username."' AND email='".$email."'";
$db->query($sql);
$db->next_record();
if ($db->f(0)!="")
{
$sql = "UPDATE user ";
$sql .="SET password='".$this->password."'";
$sql .= " WHERE id = '".$db->f(0)."'";
$db->query($sql);
//invia mail con account
mail($db->f(1), "Recupera password", "Username:".$db->f(2)."\nPassword:".$db->f(2));
mail(EMAIL, "Recupera password", "Recupera password per l'agente: Username:".$db->f(2)."\n");
return "OK";
}
else {
//utente non esiste
return "KO";
}
}
?>
----
----parte del login.php
<?php
include("includes/config.php");
$user = new User();
$user->auth();
if (isset($_SESSION["id_acc"]))
{
header('location:index.php');
}
if (isset($_POST['action']))
{
$notifica = "ATTENZIONE: Username e/o password errate";
}
?>