Alla fine ho sono riuscito facendo cosi:
Pagina form login login.php:
Codice PHP:
<?php
error_reporting (E_ALL);
if(isset($_GET['action']) && $_GET['action'] == "LoginFail") {
$msg = "Errore! Immettere nome utente e password corretti!";
}
if(isset($_GET['action']) && $_GET['action'] == "LogOut") {
$msg = "Logout effettuato con successo!";
}
if(isset($_POST['Submit']) && $_GET['action'] == "Login") {
include"config.inc.php";
include"function.inc.php";
$user = $_POST['usr'];
$pass = $_POST['pwd'];
checkLogin($user, $pass);
}
?>
<html>
<head>
<title>Area Riservata</title>
</head>
<body>
<form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF'] . "?action=Login";?>">
<table width="400" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="200">Username:</td>
<td width="200"><input name="usr" type="text" id="usr" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="pwd" type="password" id="pwd" /></td>
</tr>
<tr>
<td></td>
<td><input type="Submit" value="Submit" name="Submit"></td>
</tr>
</table>
<?php
if(isset($msg)) {
echo $msg;
}
?>
</form>
</body>
</html>
Pagina admin admin.php
Codice PHP:
<?php
session_start();
include"function.inc.php";
verifyAuth();
if(isset($_GET['exit']) && $_GET['exit'] = "yes") {
logOut();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Admin Area</title>
</head>
Benvenuto <?php echo $_SESSION['username'];?> ora puoi amministrare il il sito.</p>
</p>
[url="admin.php?exit=yes"]LogOut[/url]</p>
</body>
</html>
Pagina funzioni function.php
Codice PHP:
<?php
// Funzione controlla username e password
function checkLogin($user, $pass) {
session_start();
$_SESSION['auth'] = 0;
include"config.inc.php";
mysql_select_db($database_name, $conn) or die ("Errore nel selezionare il database: " . mysql_error());
$sql = "SELECT * FROM tb_login WHERE lg_usr = '$user' AND lg_pwd = '$pass'";
$query = mysql_query($sql) or die ("Errore nella query" . mysql_error());
if(mysql_num_rows($query) > 0 )
{
$_SESSION['username'] = $user;
$_SESSION['auth'] = 1;
header("Location: admin.php?action=LoginOk");
exit();
} else {
header("Location: login.php?action=LoginFail");
exit();
}
}
// Funzione verifica se si é loggati
function verifyAuth() {
if(isset($_SESSION['auth']) && $_SESSION['auth'] = 1){
} else {
header("Location: login.php?action=LoginFail");
exit();
}
}
// Funzione per il logout
function logOut() {
session_start();
session_unset();
session_destroy();
header("Refresh:5; URL=login.php?action=LogOut");
echo "Logout in corso ...";
exit();
}
?>
Qualcuno mi puo gentilmente controllare se quello che ho fatto va bene o potrei migliorare il tutto? Per esempio le funzioni sono corrette? è la prima volta che uso le funzioni e non so se sono giuste!!
Funziona tutto senz errori pero volevo un parere da qualcuno più esperto di me!!
Grazie Michel