 | | Offres d'emplois |  | developpeur web Vous souhaitez intégrer une équipe dynamique et créative ? Nous recherchons développeur(euse) pour créer des sites internet à Lille. Connaissances ... | |
 | | Avez vous lu ? |  | | |
|
 Authentification par cookie | |  |

<?php
define("LOGIN", "astuces");
define("PASSWD", "php");
$action = (isset($_REQUEST['action'])
? $_REQUEST['action'] : '');
function make_cookie($nom, $valeur, $expire='') {
if($expire == '') {
setcookie($nom, $valeur, mktime(0,0,0, date("d"),
date("m"), (date("Y")+1)));
}
else {
setcookie($nom, '', mktime(0,0,0, date("d"),
date("m"), (date("Y")-1)));
}
}
/*
Fonction identification() :
permet d'identifier un utilisateur
affiche le formulaire si pas l'utilisateur
n'est pas identifié
*/
function identification() {
$log = (isset($_POST['login']) ? $_POST['login'] : '');
$mdp = (isset($_POST['pws']) ? $_POST['pws'] : '');
$mode = (isset($_POST['mode']) ? $_POST['mode'] : '');
if($log == '' && $mdp == '' && $mode == '') {
$log = (isset($_COOKIE['login'])
? $_COOKIE['login'] : '');
$mdp = (isset($_COOKIE['pws']) ? $_COOKIE['pws'] : '');
$mode = (isset($_COOKIE['mode'])
? $_COOKIE['mode'] : '');
}
if (strlen($log) > 0 && strlen($mdp) > 0
&& strlen($mode) > 0) {
switch($mode) {
case"0":
if(count($_POST) > 0) {
$f_pws = md_sign($mdp);
}
else {
$f_pws = $mdp;
}
if(LOGIN == $log && md_sign(PASSWD) == $f_pws) {
make_cookie('login', $log);
make_cookie('pws', $f_pws);
make_cookie('mode', 0);
user_main($log);
}
else {
identification_form(1);
}
break;
case"1":
if(count($_POST) > 0) {
$f_pws = sha1($mdp);
}
else {
$f_pws = $mdp;
}
if(LOGIN == $log && sha1(PASSWD) == $f_pws) {
make_cookie('login', $log);
make_cookie('pws', $f_pws);
make_cookie('mode', 1);
user_main($log);
}
else {
identification_form(1);
}
break;
}
}
else {
identification_form(0);
}
}
/*
Fonction identification_form() :
affiche un formulaire d'identification
affiche un message d'erreur si l'utilisateur
n'est pas identifié.
*/
function identification_form($error=0) {
if(strlen($error) > 0) {
if($error == 0) {
$msg = "Vous devez vous identifié.<br>";
}
elseif($error == 1) {
$msg = "Votre login ou mot de passe
n'est pas valide.<br>";
}
}
$txt = '<table align="center" width="50%" border="0"
cellspacing="5">';
$txt .= '<form method="post" action="'
. $_SERVER['PHP_SELF'] . '">';
$txt .= '<input type="hidden" name="action"
value="identification">';
$txt .= '<tr>';
$txt .= '<td colspan="2" align="center">'
. $msg . '</td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td>Mode :</td>';
$txt .= '<td>';
$txt .= '<select name="mode">';
$txt .= '<option value="0" selected>MD5</option>';
$txt .= '<option value="1">SHA1</option>';
$txt .= '</select>';
$txt .= '</td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td>Login :</td>';
$txt .= '<td><input type="text" name="login"></td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td>Mot de passe :</td>';
$txt .= '<td><input type="password" name="pws"></td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td colspan="2"><hr></td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td colspan="2" align="center"><input
type="submit"
name="Submit" value="s\'identifier"></td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td colspan="2" align="center">
Login : astuces<br>Mot de passe : php
</td>';
$txt .= '</tr>';
$txt .= '</form>';
$txt .= '</table>';
echo $txt;
}
/*
Fonction user_main() :
affiche une interface après identification
*/
function user_main($login='') {
$mode = (isset($_COOKIE['mode']) ? $_COOKIE['mode'] : 0);
$log = (isset($_COOKIE['login']) ? $_COOKIE['login']
: $login);
$txt = '<table width="100%" border="0" cellspacing="5">';
$txt .= '<tr>';
$txt .= '<td align="center">';
$txt .= 'Bonjour, <b>' . $log
. '</b> Vous êtes maintenant identifié';
$txt .= '<br>Mode d\'identification actuel : ';
switch($mode) {
case"0":
$txt .= 'MD5';
break;
case"1":
$txt .= 'SHA1';
break;
}
$txt .= '</td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td align="center"> </td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td align="center">';
$txt .= '<a href="' . $_SERVER['PHP_SELF']
. '">[Actualiser]</a> ';
$txt .= '<a href="' . $_SERVER['PHP_SELF']
. '?action=delete">[Vous n\'êtes pas <b>'
. $log . '</b> ?]</a> ';
$txt .= '<a href="' . $_SERVER['PHP_SELF']
. '?action=UpForm">[Changer de mode]</a>';
$txt .= '</td>';
$txt .= '</tr>';
$txt .= '</table>';
echo $txt;
}
/*
Fonction delete() :
rend les cookies obsoletes,
affiche le formulaire d'identification
*/
function delete() {
make_cookie('login', '', 1);
make_cookie('pws', '', 1);
make_cookie('mode', '', 1);
identification_form();
}
/* Fonction UpForm() :
affiche un formulaire qui permet de changer
la signature du mot de passe
Choix entre md5() et sha1()
*/
function UpForm($msg='Changez la signature de
votre mot de passe') {
$txt = '<table align="center" width="50%" border="0"
cellspacing="5">';
$txt .= '<form method="post" action="'
. $_SERVER['PHP_SELF'] . '">';
$txt .= '<input type="hidden" name="action"
value="UpSign">';
$txt .= '<tr>';
$txt .= '<td colspan="2" align="center"><b>'
. $msg
. '</b></td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td>Votre Mot de passe :</td>';
$txt .= '<td><input type="password" name="pws"></td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td>Mode d\'identification :</td>';
$txt .= '<td>';
$txt .= '<select name="mode">';
$txt .= '<option value="0"' . (isset($_COOKIE['mode'])
&& $_COOKIE['mode'] == 0 ? ' selected' : '')
. '>MD5</option>';
$txt .= '<option value="1"' . (isset($_COOKIE['mode'])
&& $_COOKIE['mode'] == 1 ? ' selected' : '')
. '>SHA1</option>';
$txt .= '</select>';
$txt .= '</td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td colspan="2"><hr></td>';
$txt .= '</tr>';
$txt .= '<tr>';
$txt .= '<td colspan="2" align="center">
<input type="submit" name="submit" value="Modifier">
</td>';
$txt .= '</tr>';
$txt .= '</form>';
$txt .= '</table>';
echo $txt;
}
/*
Fonction UpSign() :
Permet de mettre à jour la valeur de $_COOKIE['mode']
permet de changer la signature enregistrée dans le cookie
*/
function UpSign() {
$mode = (isset($_POST['mode']) ? $_POST['mode'] : '');
$pws = (isset($_POST['pws']) ? $_POST['pws'] : '');
$old_mode = $_COOKIE['mode'];
switch($old_mode) {
case"0";
if(md5($pws) == $_COOKIE['pws']) {
if($mode == 0) {
make_cookie('pws', md_sign($pws));
}
else {
make_cookie('pws', sha1($pws));
}
}
else {
UpForm('Le mot de passe saisi ne correspond pas');
exit;
}
user_main($_COOKIE['login']);
break;
case"1":
if(sha1($pws) == $_COOKIE['pws']) {
if($mode == 0) {
make_cookie('pws', md_sign($pws));
}
else {
make_cookie('pws', sha1($pws));
}
}
else {
UpForm('Le mot de passe saisi ne correspond pas');
exit;
}
user_main($_COOKIE['login']);
break;
}
make_cookie('mode', $mode);
}
switch($action) {
case"identification":
identification();
break;
case"identification_form":
identification_form();
break;
case"user_main":
user_main();
break;
case"UpForm":
UpForm();
break;
case"UpSign":
UpSign();
break;
case"delete":
delete();
break;
default:
identification();
break;
}
?>
|
|
|
 |
|