Zend - The PHP Company




Security

Add Code


Make .htpasswd  

Type: code fragment
Added by: jochems
Entered: 22/04/2001
Last modified: 04/12/2000
Rating: **** (6 votes)
Views: 12165
If you are making a admin tool for your site you can add all the users into a table called e.g. "users" and automaticaly generate a .htpasswd file from it. This function uses MD5 encryption.


<?php
function makehtaccess() {
  global 
$Cfg;
  
connect();
  
$users mysql_query("select username,pass from users where access > '0'") or die ("Can't get users (makehtaccess)");
  
$num mysql_numrows($users);
  
$t 0;
  if (
$num != 0) {
    
$f fopen(".htpasswd""w+");
    while (
$num>$t) {
          unset(
$seed);
      
# generate random seed
      
$all explode" ",  
        
"a b c d e f g h i j k l m n o p q r s t u v w x y z " 
      
"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z " 
        
"0 1 2 3 4 5 6 7 8 9");
      for(
$i=0;$i<9;$i++) { 
        
srand((double)microtime()*1000000); 
        
$randy rand(061); 
        
$seed .= $all[$randy];
      }
      
$username mysql_result($users,$t,"username");
      
$password mysql_result($users,$t,"pass");
      
$crypt crypt($password"$1$$seed");
      
fputs($f,"$username:$cryptn");
      
$t++;
    }
  }
}
?>


Usage Example


Table layout:

CREATE TABLE users(
   username varchar(30) NOT NULL,
   pass varchar(40) NOT NULL,
   access int(1) DEFAULT '0' NOT NULL,
);


Rate This Script





Search



This Category All Categories