info:myphp:identification

PHP authentification / authentication / identification

<?php
/* fake htaccess authentication 
Fred Radeff (aka FR), radeff@akademia.ch, radeff.red, 21.02.2007
adapted from: http://www.grappa.univ-lille3.fr/polys/reseaux-2004/reseaux020.html

note:
if you want to keep a trace of the logins, put a file login.txt
in the same directory, which will be writable by the webserver or chmod 777
and uncomment lines after "//authentication ok, keep a trace in file login.txt"
*/

/*restricted user list (login/password) */
$liste=array(
"toto/toto",
"mao.zedong@china.com/manifest",
"fred/fred"
);
  #create table from array $liste
    for ($i=0;$i<count($liste);$i++) {
      $l=explode("/",trim($liste[$i]));
      $user[$i]=$l[0]; 
      $pass[$i]=$l[1]; 
    }
    $nbusers=count($liste);
    //check ID
    $ok=-1; //start with no ID
    for ($i=0;$i<$nbusers;$i++) {
      if (($_SERVER['PHP_AUTH_USER']==$user[$i]) && ($_SERVER['PHP_AUTH_PW']==$pass[$i])) {
        //USER ok, keep id
        $ok=$i;
      }
    }
 //if check KO, $ok is still -1
    //ask login+password
    if ($ok==-1) {
	header("WWW-Authenticate: Basic realm='Restricted Area'");
	print("<H1>Authorization Required</H1>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.<hr>");
	exit;      
    }
    //authentication ok, keep a trace in file login.txt
//uncomment if you want to keep a trace
/*	$filename = 'login.txt';
if (!$handle = fopen($filename, 'a')) {
         echo "Impossible d'ouvrir le fichier ($filename)";
         exit;
   }
	$somecontent = $_SERVER['PHP_AUTH_USER'] .";" .date("Y-m-d h:i:s") ."\n";
	fwrite($handle, $somecontent);
	fclose($handle);
*/
/*OK end authentication */

?>
<?
/*  Test du nom d'utilisateur et du mot de passe  */

if(($PHP_AUTH_USER == $username) AND ($PHP_AUTH_PW == $password))
{
include $fichier;
 }
else
{
/* Envoie des en-t<EA>tes obligeant le navigateur <E0> exiger
    de l'utilisateur un nom et un mot de passe valides        */
header("WWW-Authenticate: Basic realm=\"emacs\"");
header("HTTP/1.0 401 Unauthorized");
/* Message d'erreur en cas d'entr<E9>e invalide */
print("<H1>Authorization Required</H1>");
}
?>
  • info/myphp/identification.txt
  • Dernière modification : 2026/03/05 09:51
  • de radeff