la voici:
index.php
<?php
require_once('auth.php');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Acceuil</title>
</head>
<body leftmargin="0" topmargin="0">
<div id="header">
<ul id="navbar">
<li><a title="Acceuil" href="index.php">Acceuil</a></li>
<li><a title="visualiser" href="visualiser.php">Graphiques</a></li>
<li><a title="Lien" href="#">Nom de la rubrique</a></li>
<li><a title="Lien" href="#">Nom de la rubrique</a></li>
</ul>
</div>
</body>
</html>
login.php
<?php
require_once('auth.class.php');
require_once('db.inc.php');
$username=GetParam($_REQUEST,'username','');
$password=GetParam($_REQUEST,'password','');
$message='';
if ($username!="")
{
//Authenticate
$auth=new UserAuth($database);
$auth->Logout();
if ($auth->Login($username,$password))
{
header('location: index.php');
exit;
}
else
{
$message='Login incorrect';
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Login</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="180" align="left" valign="top" rowspan=2>
<table class="menuleft" height="400" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td>
</td></tr>
<td width="607" valign="top">
<center>
<?php if ($message!=''){ ?>
<table cellspacing="0" cellpadding="0">
<tr>
<td width="100%" height="25" colspan="2">
<strong><?=$message?></strong>
</td>
</tr>
</table >
<?php } ?>
<form action="login.php" method="post" >
Username:<input type="text" name="username" value=""><br>
Password:<input type="password" name="password" value=""><br><br>
<input type="submit" name="sumbit" value="Login">
</form>
</td>
</tr>
</table>
</body>
</html>
auth.class.php
<?php
require_once('database.php');
class UserAuth {
var $_db=null;
var $userid=null;
var $username=null;
var $usercookie=null;
var $sessioncookie=null;
var $session_id=null;
function UserAuth(&$db) {
$this->_db =& $db;
}
function Login($user,$pass){
if (!$user || !$pass) {
return FALSE;
}
$this->_db->setQuery( "SELECT * "
. "\nFROM user "
. "\nWHERE username='$user' AND password='$pass' and status='active'"
);
$row = null;
if (!$this->_db->loadObject( $row )) {
return FALSE;
}
if (defined( '_ACL_ADMIN' )) {
if ($row->vip!='1') return FALSE;
}
$lifetime = time() + 365*24*60*60;
setcookie( "usercookie[username]", $user, $lifetime, "/" );
setcookie( "usercookie[id]", $row->id, $lifetime, "/" );
$this->initSession();
$this->_db->setQuery( "update session "
. "\n set "
. "\n userid='$row->id',"
. "\n sess_start=now(),"
. "\n sess_expire=DATE_ADD(now(),INTERVAL ".(_DEFAULT_TIMEOUT)." MINUTE),"
. "\n last_activity=now(),"
. "\n ip='".$_SERVER['REMOTE_ADDR']."',"
. "\n user_agent='".$_SERVER['HTTP_USER_AGENT']
. "' where session='$this->session_id'"
);
$this->_db->query();
$this->userid=$row->id;
$this->username=$user;
$this->usercookie['username']=$user;
$this->usercookie['id']=$row->id;
return TRUE;
}
function Logout(){
if ($this->Check_Auth()) {
$this->_db->setQuery( "delete from session where session='$this->session_id'");
$this->_db->query();
}
setcookie( "usercookie[username]", "", time() - 36000, "/" );
setcookie( "usercookie[id]", "", time() - 36000, "/" );
setcookie( "sessioncookie", "", time() - 36000, "/" );
}
function generateId() {
$failsafe = 20;
$randnum = 0;
while ($failsafe--) {
$randnum = md5( uniqid( microtime(), 1 ) );
if ($randnum != "") {
$cryptrandnum = md5( $randnum );
$this->_db->setQuery( "SELECT * FROM session WHERE session=MD5('$randnum')" );
if(!$result = $this->_db->query()) {
die( $this->_db->stderr( true ));
// todo: handle gracefully
}
if ($this->_db->getNumRows($result) == 0) {
break;
}
}
}
$this->sessioncookie = $randnum;
$this->session_id = md5( $randnum . $_SERVER['REMOTE_ADDR'] );
}
function initSession() {
$sessioncookie = GetParam( $_COOKIE, 'sessioncookie', null );
$sess=md5( $sessioncookie . $_SERVER['REMOTE_ADDR'] );
$row = null;
$this->_db->setQuery("select * from session where session='$sess'");
if ($this->_db->loadObject( $row )) {
// Session cookie exists, update time in session table
$this->_db->setQuery("update session set last_activity=now(), ".
"sess_expire=DATE_ADD(now(),INTERVAL ".(_DEFAULT_TIMEOUT)." MINUTE) where session='$sess'");
$this->_db->query();
$this->sessioncookie=$sessioncookie;
$this->session_id=$row->session;
} else {
$this->generateId();
setcookie( "sessioncookie", $this->sessioncookie, time() + 43200, "/" );
$this->_db->setQuery("insert into session set last_activity=now(), session='$this->session_id',".
"sess_expire=DATE_ADD(now(),INTERVAL ".(_DEFAULT_TIMEOUT)." MINUTE) ");
$this->_db->query();
}
}
function Check_Auth(){
$sess = GetParam( $_COOKIE, 'sessioncookie', null );
$ucookie = GetParam( $_COOKIE, 'usercookie', null );
$sess=md5( $sess . $_SERVER['REMOTE_ADDR'] );
$this->_db->setQuery("delete from session where now()>sess_expire"); //PURGE SESSION
$this->_db->query();
$row = null;
$this->_db->setQuery("select * from session a left join user b
on a.userid=b.id where session='$sess'");
if (!$this->_db->loadObject( $row )) {
return FALSE;
}
if (($row->id==$ucookie['id'])&&($row->username==$ucookie['username'])
&&($_SERVER['REMOTE_ADDR']==$row->ip)){
//authenticated ok
if (defined( '_ACL_ADMIN' )) {
if ($row->vip!='1') return FALSE;
}
$this->userid=$row->id;
$this->username=$row->username;
$this->usercookie['username']=$row->username;
$this->usercookie['id']=$row->id;
$this->sessioncookie = GetParam( $_COOKIE, 'sessioncookie', null );
$this->session_id = $sess;
if (!defined(_TIMEOUT_ABSOLUTE)){
$this->_db->setQuery("update session set last_activity=now(), ".
"sess_expire=DATE_ADD(now(),INTERVAL ".(_DEFAULT_TIMEOUT)." MINUTE) where session='$sess'");
$this->_db->query();
}else{
$this->_db->setQuery("update session set last_activity=now() where session='$sess'");
$this->_db->query();
}
return TRUE;
}else{
return FALSE;
}
}
}
function GetParam( &$arr, $name, $def=null ) {
$return = null;
if (isset( $arr[$name] )) {
return $arr[$name];
} else {
return $def;
}
}
?>
auth.php
<?php
require_once('auth.class.php');
require_once('db.inc.php');
$auth= new UserAuth($database);
if (!$auth->Check_Auth())
{
header('location: login.php');//bad auth!!
exit;
}
?>
db.inc.php
<?php
$db_config['server']='localhost';
$db_config['database']='rat';
$db_config['user']='root';
$db_config['pass']='';
define ('_DEFAULT_TIMEOUT',1800); //Session timeout minutes
#
# If this constant is defined, then the Timeout behaves
# relative to the logintime. that means if you login now,
# regardeless to your amount of activity you will be
# timed out in _DEFAULT_TIMEOUT minutes.
# If this is not your intension
# just comment out the following line.
define ('_TIMEOUT_ABSOLUTE',1);
require_once ('database.php');
$database = new database( $db_config['server'],
$db_config['user'], $db_config['pass'], $db_config['database'],
'' );
?>
Donc cette classe fonctionne très bien mais sur la page d'index il n'y a pas de liens de déconnection, donc j'ai décidé de le faire moi même avec un bouton
je code mon bouton fait l'appel a la fonction de déconnection et lui dis de me rebalancer vers ma page de login. Je met tout sa dans mon dossier local et je lance la page.
lorsque je clique sur mon bouton j'ai bien le rond de chargement durant quelques secondes et puis rien ne ce passe...
Est-ce que quelqu'un pourrais m'aider, car la je sèche...
je vous remercie a l'avance de votre aide
PS: est-ce qu'il aurais moyen de le faire avec un liens ?