Jump to content

.htpasswd Registration Form


mbuckley2000

Recommended Posts

Hi,

I wanted to make an htpasswd registration form.

I found this code on the internet but have no idea how to use it.

Can anyone help?

<?php

// 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";

          }

 

}

 

// Show all Users and Passwords

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);

      }

 

}

 

 

// Encrypt Passwords

function htpasswd($pass)

 

{

 

    $pass = crypt(trim($pass),base64_encode(CRYPT_STD_DES));

 

    return $pass;

 

}

 

Link to comment
https://forums.phpfreaks.com/topic/182390-htpasswd-registration-form/
Share on other sites

  • 5 years later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.