Jump to content

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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.