Astuces php
Accueil
  News 
 Scripts 
 Bouquins 
 Newsletter 
  Emplois 
 Annuaire 
 Forum 
 
Offres d'emplois
développeur php
La société Metal Hurlant recherche un(e) stagiaire possédant une solide maîtrise de PHP/MYSQL pour la réalisation d'un jeu de gestion massivement m...
 
 C'est ici
 
Avez vous lu ?
PHP 5 Astuces d'experts
Avez vous lu ? -> PHP 5 Astuces d'experts : Cet ouvrage, utile et pratique, délivre aux utilisateurs initiés des astuces toutes plus utiles les unes que les autres sur PHP 5. Il permet entre autre d’apprendre à envoyer un e-mail aux formats texte et HTML, de récupérer la date de la dernière visite d’un utilisateur, de générer un cookie, de...
 
 Commander
 
 Chapitres - Sécurité
PHP 5 Astuces d'experts -> Sécurité -> Utiliser une signature MD5 Utiliser une signature MD5
PHP 5 Astuces d'experts -> Sécurité -> Utiliser une signature SHA1 Utiliser une signature SHA1
PHP 5 Astuces d'experts -> Sécurité -> Authentification par cookie Authentification par cookie
PHP 5 Astuces d'experts -> Sécurité -> Authentification par session Authentification par session
PHP 5 Astuces d'experts -> Sécurité -> Forcer le type d'une variable Forcer le type d'une variable
PHP 5 Astuces d'experts -> Sécurité -> Utiliser des fonctions de formatage des chaînes de texte Utiliser des fonctions de formatage des chaînes de texte
 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$valeurmktime(0,0,0date("d"),
      
date("m"), (date("Y")+1)));
  }
  else {
    
setcookie($nom''mktime(0,0,0date("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) > && 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'] == ' selected' '')
      . 
'>MD5</option>';
    
$txt .= '<option value="1"' . (isset($_COOKIE['mode'])
      && 
$_COOKIE['mode'] == ' 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;
  }
?>
 Rechercher
Tapez un mot ou une phrase clé
 

 Téléchargement
Téléchargez les sources de l'ouvrage
PHP 5 solutions et composants open-source
Téléchargez les sources de l'ouvrage  -> PHP 5 solutions et composants open-source : Boostez et améliorez vos sites PHP avec des composants et extensions !

Voici enfin l'ouvrage pour améliorer votre site sans tout réécrire. Pourquoi réinventer la roue puisqu'il existe une multitude de composants indépendants à intégrer à votre site ou même des extensions au langage PHP qui peu...
 


Derniers tickets
  • bandeaux publicita... ()
  • Mail avec liste de... ()
  • pb install wamp ()
  • Bouton recalculer ()
  •  

    copyright astuces-php.info - Plan du site - Mylinea.org : l'annuaire des annuaires - échange de liens - Faites du fric