 | | Offres d'emplois |  | Développeur PHP Vous êtes rigoureux, exigant, autonome et vous avez la pêche. Vous serez en charge du développement de sites Internet/intranet/extranet ainsi que d... | |
 | | Avez vous lu ? |  | | |
|
 Authentification par session | |  |

<?php
session_name('sid');
session_start();
$session = session_name() . '=' . session_id();
define("LOGIN", "astuces");
define("PASSWD", "php");
function make_session($nom, $valeur, $expire='') {
if($expire == '') {
$_SESSION[$nom] = $valeur;
}
else {
unset($_SESSION[$nom]);
}
}
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($_SESSION['login'])
? $_SESSION['login'] : '');
$mdp = (isset($_SESSION['pws']) ? $_SESSION['pws'] : '');
$mode = (isset($_SESSION['mode'])
? $_SESSION['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_session('login', $log);
make_session('pws', $f_pws);
make_session('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_session('login', $log);
make_session('pws', $f_pws);
make_session('mode', 1);
user_main($log);
}
else {
identification_form(1);
}
break;
}
}
else {
identification_form(0);
}
}
function identification_form($error=0) {
if(strlen($error) > 0) {
if($error == 0) {
$msg = "Vous devez vous identifier.<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;
}
function user_main($login='') {
global $session;
$mode = (isset($_SESSION['mode']) ? $_SESSION['mode'] : 0);
$log = (isset($_SESSION['login'])
? $_SESSION['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']
. (isset($session) ? '?' . $session : '')
. '">[Actualiser]</a> ';
$txt .= '<a href="' . $_SERVER['PHP_SELF']
. '?action=delete' . (isset($session) ? '&'
. $session : '') . '">[Vous n\'êtes pas <b>'
. $log . '</b> ?]</a> ';
$txt .= '<a href="' . $_SERVER['PHP_SELF']
. '?action=UpForm’ . (isset($session) ? '&'
. $session : '') . '">[Changer de mode]</a>';
$txt .= '</td>';
$txt .= '</tr>';
$txt .= '</table>';
echo $txt;
}
function delete() {
make_session('login', '', 1);
make_session('pws', '', 1);
make_session('mode', '', 1);
identification_form();
}
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($_SESSION['mode'])
&& $_SESSION['mode'] == 0 ? ' selected' : '')
. '>MD5</option>';
$txt .= '<option value="1"' . (isset($_SESSION['mode'])
&& $_SESSION['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;
}
function UpSign() {
$mode = (isset($_POST['mode']) ? $_POST['mode'] : '');
$pws = (isset($_POST['pws']) ? $_POST['pws'] : '');
$old_mode = $_SESSION['mode'];
switch($old_mode) {
case"0";
if(md_sign($pws) == $_SESSION['pws']) {
if($mode == 0) {
make_session('pws', md_sign($pws));
}
else {
make_session('pws', sha1($pws));
}
}
else {
UpForm('Le mot de passe saisi ne correspond pas');
exit;
}
user_main($_SESSION['login']);
break;
case"1":
if(sha1($pws) == $_SESSION['pws']) {
if($mode == 0) {
make_session('pws', md_sign($pws));
}
else {
make_session('pws', sha1($pws));
}
}
else {
UpForm('Le mot de passe saisi ne correspond pas');
exit;
}
user_main($_SESSION['login']);
break;
}
make_session('mode', $mode);
}
?>
|
|
|
 |
|