Ciao A tutti,
mi trovo alle prese con un sistema di login per un sito di stampe online,
fin quando ho lavorato in locale il sitema tramite PDO non mi dava nessun problema ma la momente del trasferimento del sito sul server ecco che dopo la registrazione il sistema di login non mi fa effettuare l'accesso.
Vi posto il codice.
grazie in anticipo per la disponibilità.
Forn di accesso:
codice:
<form id="accesso" name="contact" method="post" action="autentica.php">
<input id="username" type="text" name="user" value="" />
<input id="password" type="password" name="pass" value="" />
<input type="submit" id="submit" value="LOGIN">
</form>
Autentica.php
Codice PHP:
<?php
require_once "autenticazione/Utente.php";
session_start();
$_SESSION["user"] = new Utente($_POST["user"], $_POST["pass"]);
$_SESSION["user"]->autentica();
if ($_SESSION["user"]->isAutenticato()) {
header("Location: /xxx/index.php");
} else {
header("Location: /xxx/index.php?loginFallito=1");
}
?>
autenticazione/Utente.php
Codice PHP:
<?php
class Utente {
/**
*
* @var string Il nome utente
*/
private $nome;
/**
*
* @var string La password, sia in chiaro che codificata
*/
private $password;
/**
*
* @var bool Lo stato di autenticazione dell'utente
*/
private $autenticato;
/**
*
* @var bool
*/
private $passwordCrittata;
/**
*
* @var array Contiene gli eventuali messaggi di errore in caso di
* autenticazione fallita.
*/
private $errori;
public function __construct($nome, $password) {
$this->nome = $nome;
$this->password = $password;
$this->autenticato = false;
$this->passwordCrittata = false;
$this->errori = array();
}
public function autentica() {
try {
$db = new PDO("mysql:host=localhost;dbname=xxx",
"root",
" ",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = sprintf("SELECT * FROM admin WHERE `nome` = '%s'", addslashes($this->nome));
$comandoSQL = $db->query($sql);
$risultato = $comandoSQL->fetch(PDO::FETCH_ASSOC);
unset($db);
} catch (PDOException $e) {
$this->errori[] = $e;
}
if ($risultato) {
if (!$this->passwordCrittata) {
$this->password = hash($risultato["algoritmo"], $this->password, true);
$this->passwordCrittata = true;
}
$this->autenticato = $this->password == $risultato["password"];
if (!$this->autenticato)
$this->errori[] = "Le password non corrispondono.";
} else {
$this->errori[] = "Utente non trovato.";
}
}
public function isAutenticato() {
return $this->autenticato;
}
public function __toString() {
return $this->nome;
}
}
?>
Questa invece è la query per l'inserimento dei dati:
Codice PHP:
try{
$sql = "INSERT INTO `admin` (`id`, `nome`, `password`)
VALUES ( '$id', '$mail', SHA1('$pass'))";
//Preparo le strutture per contenere la query
$stmt = $dbh->prepare($sql);
//Controllo se la query è andata a buon fine
if (! $stmt->execute() ) echo "[b]Errore nella query.[/b]";
}
//Nel caso in cui ci siano errori, li cattura nella variabile $myerror e li stampa
catch (PDOException $myerror)
{
print "Problemi nella connessione al database:
" . $myerror->getMessage() . "
";
}