Ok allora il codice completo che potrai tranquillamente modificare si compone dei seguenti files :
- users.php
- functs.php
- login.php
- logout.php
- checksession.php
- index.html
- test-home.html
USERS.PHP
Codice PHP:
<?
/*
USERS DEFINITIONS FILE protect this with htaccess
*/
$users = array(
"gianni" => array(
"pwd" => "aiuhajksd" ,
"homepage" => "gianni_home.php" ,
"is_active" => true
),
"test" => array(
"pwd" => "test" ,
"homepage" => "test_home.php" ,
"is_active" => true
),
"marco" => array(
"pwd" => "aiasdsd" ,
"homepage" => "marco_home.php" ,
"is_active" => true
)
);
?>
FUNCTS.PHP
Codice PHP:
<?
/* FUNCTS FILE */
function logga($str)
{
$path = "./logs/";
// Create the log dir if not exists
if( !file_exists($path) || !is_dir($path) )
{
mkdir($path);
chmod($path , 0777);
}
// Create the filename log , if file exists open in append mode
$logFile = $path.date( "Y-m-d" , time() )."_access_log.txt" ;
$fp = fopen($logFile,"a") ;
if( !$fp )
{ //IF fail to open or create file track the error
return false ;
}
$str .= "\n" ;
// Write content in the file
fwrite( $fp , $str , strlen($str) ) ;
// Close the file
fclose($fp);
return true;
}
?>
LOGIN.PHP
Codice PHP:
<?
/*LOGIN FILE*/
require_once("users.php") ;
require_once("functs.php") ;
$lgn = $_POST["login"] ;
$pwd = $_POST["password"] ;
$user = $users[$lgn] ;
$uip = $_SERVER['REMOTE_ADDR'];
$atime = date( "Y-m-d H:i:s" , time() ) ;
$ua = $_SERVER["HTTP_USER_AGENT"] ;
$str =
"
--------------------------------------------------------------------------------
[".$atime."] LOGIN ATTEMPT
FROM : ".$uip."
USER-AGENT : ".$ua."";
if( $user && $user["pwd"]==$pwd && $user["is_active"]===true )
{
$str .=
"
RESULT : data accepted => user ".$lgn."
--------------------------------------------------------------------------------";
// STARTUP AND STORE THE SESSION VARS FOR USER ACCESS CONTROLS
session_start() ;
$_SESSION["logged_user"] = $lgn ;
// Trick to hide pwd data
$_SESSION["logged_password"] = md5($pwd) ;
if( !logga($str) )
{
session_unset();
session_destroy();
// IF LOG FAILS, FORBID USER ACCESS
die("Impossibile gestire i dati di accesso") ;
}
header("location:".$user["homepage"]);
}
else
{
$str .=
"
RESULT : login failed => user ".$lgn." , used pwd => ".$pwd."
--------------------------------------------------------------------------------";
if( !logga($str) )
{
die("Impossibile gestire i dati di accesso") ;
}
die("TU NON PUOI PASSSHARE !!! [cit]Gandalf[/cit] ");
}
?>
LOGOUT.PHP
Codice PHP:
<?
session_start();
session_destroy();
header("location:./index.html");
die();
?>
CHECKSESSION.PHP
Codice PHP:
<?
require_once("./users.php") ;
require_once("./functs.php") ;
session_start();
$lgn = $_SESSION["logged_user"] ;
$pwd = $_SESSION["logged_password"] ;
$user = $users[$lgn] ;
if( !$user || md5($user["pwd"])!=$pwd || $user["is_active"]!==true )
{
header("location:./logout.php");
die();
}
?>
INDEX.HTML
Codice PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, [url]www.pspad.com[/url]">
<title></title>
</head>
<body>
<form name="logga" action="./login.php" method="post">
Login: <input type="text" name="login" />
Password : <input type="password" name="password" />
<input type="submit" value="login" />
</form>
</body>
</html>
TEST_HOME.PHP
Codice PHP:
<?
include("./checksession.php");
// SPECIFIC USER ACCESS CONTROL
if($_SESSION["logged_user"]!="test")
{ die("NON PUOI ACCEDERE A QUESTA PAGINA"); }
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, [url]www.pspad.com[/url]">
<title></title>
</head>
<body><center>
<h1 >PAGINA DI : TEST</h1>
[url="./logout.php"]ESCI[/url]
</center></body>
</html>
Provalo .... ciao.