DSN='sqlite:users.sqlite' ;
$myLogin->DBTABLE='users';
$myLogin->FIELDID='id';
$myLogin->FIELDUSER='user';
$myLogin->FIELDPASS='pass';
$myLogin->loginError='You are not a valid user!
';
if ($myLogin->LoginSession(true, true)==true) //LoginSession($form=true, $autoExit=true)
{
echo 'Logged in, success! (Logout).
';
}else{
echo 'You are not logged in!
';
//exit(1);
}
// If "$autoExit=true" the underlying html block is returned only in case of successful authentication.
echo'
Login
';
?>
===== La nostra classe =====
**sauth.php**
loginError=='')
$this->loginError='Wrong password or username
';
if($this->headerloginform=='')
{
$this->headerloginform='
Login
';
}
if($this->loginform=='')
{
$this->loginform='
';
}
}
function LoginSession($form=true, $autoExit=true)
{
$AuthErrorMessage=$this->loginError;
$rowcount=0;
try
{
$dbh = new PDO($this->DSN, $this->DBUSER, $this->DBPASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $myerror)
{
print "Database error:
" . $myerror->getMessage() . "
";
}
@$sql="select * from ". $this->DBTABLE. " where ".$this->FIELDUSER."='".$_SESSION[$this->SESSIONUSER]. "' and ".$this->FIELDPASS."='".$_SESSION[$this->SESSIONPASS]."'";
$stmt = $dbh->prepare($sql);
//Controllo se la query รจ andata a buon fine
if (! $stmt->execute() ) echo 'Error in query '.$sql.'
';
//conto il numero di righe restituite dalla query
while ($row = $stmt->fetch(PDO::FETCH_BOUND)) $rowcount++;
if ($rowcount!=0)
{
//Logged in, success!
$Error="";
$dbh = null;
return true;
}else{
if (!isset($_POST["login"]) )
{
if ($form==true) @$this->MakeHtmlForm($Error);
}else{
$sql="select * from ".$this->DBTABLE. " where ".$this->FIELDUSER."='".$_POST["user"]. "' and ".$this->FIELDPASS."='".$_POST["pass"]."'";
$stmt = $dbh->prepare($sql);
if (! $stmt->execute() ) echo'Error in query
';
$_SESSION[$this->SESSIONUSER]=$_POST["user"] ;
$_SESSION[$this->SESSIONPASS]=md5($_POST["pass"]);
$sql="select * from ". $this->DBTABLE. " where ".$this->FIELDUSER."='".$_POST["user"]. "' and ".$this->FIELDPASS."='".md5($_POST["pass"])."'";
$stmt = $dbh->prepare($sql);
if (! $stmt->execute() ) echo'Error in query
';
//Prelevo lo userid
foreach ($dbh->query($sql) as $row)
{
$_SESSION[$this->SESSIONUSERID]=$row[$this->FIELDID];
continue;
}
//Conto i record
$rowcount=0;
while ($row = $stmt->fetch(PDO::FETCH_BOUND)) $rowcount++;
if ($rowcount!=0)
{
//Se sta qui e' autenticato, ed esce dalla funzione auth();
$dbh = null;
return true;
}else{
// credenziali sbagliate
$Error=$AuthErrorMessage;
if ($form==true) $this->MakeHtmlForm($Error);
$dbh = null;
if ($autoExit==true) exit(1);
return false;
}
}
//Non autenticato, cancella le variabili di sessione e esce.
@session_start();
@session_destroy();
if ($autoExit==true) exit(1);
return false ;
}
}
function MakeHtmlForm($Error)
{
//Render form for login
echo $this->headerloginform;
echo $Error;
echo $this->loginform;
}
}//fine classe
?>
===== Realizzare la pagina per il logout =====
Per realizzare la pagina che faccia il logout e' sufficiente distruggere la sessione con il seguente codice:
**logout.php**
Login
Logout effettuato correttamente
fai di nuovo il login
';