PHP functions to Apache handle.htpasswd file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
< ?php
/*
 
 Author : Mitko Kostov
 Weblog : http://mkostov.wordpress.com 
 Mail :mitko.kostov@gmail.com
 Date : 12/21/2007
 Used : PHP and Smarty
 
*/
// function to register user
function regUser() {      
        $filename = 'members/password/.htpasswd';
        $data = $_POST['username'].":".htpasswd($_POST['password'])."\n";
   if (is_writable($filename)) {   
            if (!$handle = fopen($filename, 'a')) {
                echo "Cannot open file ($filename)";
                exit;
            }   
            if (fwrite($handle, $data) === FALSE) {
                echo "Cannot write to file ($filename)";
                exit;
            }
            // echo "Success, wrote ($data) to file ($filename)";
            fclose($handle);
         } else {        
            echo "The file $filename is not writable";
           }
}
 
// function to show all users and passwords ( encrypted )
function showUser()
{    
     $file = file('members/password/.htpasswd');
     $array = array();
     $count = count($file);
     for ($i = 0; $i < $count; $i++)
     {
                list($username, $password) = explode(':', $file[$i]);
                $array[] = array("username" => $username, "password" => $password);
      }
 
     return $array;
}
function delUser($username) { 
   $fileName = file('members/password/.htpasswd');
   $pattern = "/". $username."/";  
   foreach ($fileName as $key => $value) {   
   if(preg_match($pattern, $value)) { $line = $key;  }
   } 
  unset($fileName[$line]); 
   if (!$fp = fopen('members/password/.htpasswd', 'w+'))
      {  
        print "Cannot open file ($fileName)";     
        exit;
      }
 
     if($fp)
      {       
        foreach($fileName as $line) { fwrite($fp,$line); }       
        fclose($fp);
      } 
}
 
// function for encrypting password   
function htpasswd($pass)
{
     $pass = crypt(trim($pass),base64_encode(CRYPT_STD_DES));
     return $pass;
}
?>

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru

Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

Sign: umsun Hello!!! rcuwwymhyw and 2006ssgfhphzye and 4119I love your blog. :) I just came across your blog.

Americans at private meeting to discuss philanthropy- included was David Rockefeller Sr. ,

Sign: zdbrw Hello!!! euvnq and 9111rgkwwrxapd and 2388 : I will try to recommend this post to my friends and family, cuz its really helpful.

ancient greek fashion back in style http://foreverfashion.eu/hermes/shoulder-bags-&-totes_g40.html 70s top fashion

thank you for this very useful blog hope to see it grow bigger soon

Best Site good looking lolita cp message board wnw

No, I’m not particularly sporty Top 100 Nn Teen Model
5615

I can’t stand football Very Young Nn
:O

How would you like the money? Pretee Nn
>:PPP

Leave a comment

(required)

(required)