home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / httpauth.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  2.7 KB  |  101 lines

  1. http authentication logoff 
  2.  
  3. A reliable method of logging people off via http authentication. It has been tested on ns3,4 and ie4,5. It's just a skeleton, you'll need to fill in the database requests yourself. 
  4.  
  5.  
  6. <?php
  7.   // this is the logoff script which will simply set the
  8.   // loggedon variable of the user to 0.
  9.   // This will cause the next password check of the user
  10.   // to fail.
  11.   // the previous script will then send the Deny message, with
  12.   // a $realm which contains a timestamp. This means that the
  13.   // browser will get a realm which it doesn't know. Therefor, it
  14.   // will not have any default user login/password to go with it.
  15.   // this prevents IE4,5 from trying it once again, without user 
  16.   // intervention or displaying the login/password in the login
  17.   // window.
  18.   // 
  19.   // The authentication routine for this script is the same
  20.   // as for any other page you wish to protect.
  21.   
  22. class puser {
  23.  
  24.   function CheckPassword($password) {
  25.  
  26.     if (!$this->loggedon) {
  27.       // fail once for users who have logged off.
  28.       // I've used a timestamp here, so you could also
  29.       // decide that users should login again after a
  30.       // a certain amount of time.
  31.       $this->loggedon=time();
  32.       $this->save();
  33.     } else {
  34.       $password=crypt($password, substr($this->password, 0, 2));
  35.     }
  36.     return $password==$this->password; 
  37.   }
  38.  
  39.   function save() {
  40.     // save user data somewhere, preferable a database
  41.     // this includes login, password and loggedon 
  42.  
  43.     // ... fill in
  44.  
  45.   }
  46.  
  47. }
  48.  
  49. function Deny($realm, $accesdenied) {
  50.   Header("WWW-Authenticate: Basic Realm=\"".$realm."\"");
  51.   Header("HTTP/1.0 401 Unauthorized");
  52.   echo $accessdenied;
  53.   return 0;
  54. }
  55.  
  56. function GetUser($login) {
  57.   // find the user data and instantiate a user 
  58.   // object if found.
  59.  
  60.   // ... fill in
  61.  
  62.   if ($login_found) {
  63.     $user=new user;
  64.     $user->login=$login;  
  65.     $user->loggedon=$loggedon;
  66.     $user->password=$password;
  67.   }
  68.   return $user;
  69. }
  70.  
  71. // important! the timestamp in the realm is the trick
  72. // to a reliable logoff. 
  73. $realm="A Realm (".strftime("%c",time()).")";
  74. $accesdenied="<html><body bgcolor=\"white\"><h1>".
  75.              "Access Denied</h1></body></html>";
  76.  
  77. if (!$PHP_AUTH_USER) {
  78.   // no authentication data -> deny access.
  79.   Deny($realm, $accesdenied);
  80. } else {
  81.   $user=GetUser($PHP_AUTH_USER);
  82.   if ($user && $user->CheckPassword($PHP_AUTH_PW)) {
  83.     // the user is in, display your page here. 
  84.     // or in this case, as it is the logoff page:
  85.     $user->loggedon=0;
  86.     $user->save();
  87. ?>
  88. <html>
  89. <body>
  90.   <h3>Goodbye <?php echo $user->login; ?>, come back soon.</h3>
  91. </body>
  92. </html>
  93. <?php
  94.   } else {
  95.     Deny($realm, $accesdenied);
  96.   }
  97. }
  98.  
  99. ?>
  100.  
  101.