Home > Backend Development > PHP Tutorial > PHP class to parse .htpasswd file in linux_PHP tutorial

PHP class to parse .htpasswd file in linux_PHP tutorial

WBOY
Release: 2016-07-20 11:10:52
Original
856 people have browsed it

Friends who need the PHP class for parsing .htpasswd files in Linux can refer to it.

Introduce a
method of use

$

$passwdHandler -> addUser('user1', 'I prefer to use passphrase rather than password.'); // Delete the user 'user1' if it exists in .htpasswd.
The code is as follows
 代码如下 复制代码
passwdHandler = new Htpasswd('/home/myuser/.htpasswd');
// Add a user with name 'user1' and password 'I prefer to use passphrase rather than password.' if it doesn't exist in .htpasswd.
$passwdHandler -> addUser('user1', 'I prefer to use passphrase rather than password.');
// Delete the user 'user1' if it exists in .htpasswd.
$passwdHandler -> deleteUser('user1');
// Check if user 'user1' exists in .htpasswd.
if ($passwdHandler -> doesUserExist('user1')) {
// User 'user1' exists.
}
Copy code

passwdHandler = new Htpasswd('/home/myuser/.htpasswd');
 代码如下 复制代码

class Htpasswd {
private $file = '';
private $salt = "AynlJ2H.74VEfI^BZElc-Vb6G0ezE9a55-Wj";
private function write($pairs = array()) {
$str = '';
foreach ($pairs as $username => $password) {
$str .= "$username:{SHA}$passwordn";
}
file_put_contents($this -> file, $str);
}
private function read() {
$pairs = array();
$fh = fopen($this -> file, 'r');
while (!feof($fh)) {
$pair_str = str_replace("n", '', fgets($fh));
$pair_array = explode(':{SHA}', $pair_str);
if (count($pair_array) == 2) {
$pairs[$pair_array[0]] = $pair_array[1];
}
}
return $pairs;
}
private function getHash($clear_password = '') {
if (!empty($clear_password)) {
return base64_encode(sha1($clear_password, true));
} else {
return false;
}
}
public function __construct($file) {
if (file_exists($file)) {
$this -> file = $file;
} else {
die($file." doesn't exist.");
return false;
}
}
public function addUser($username = '', $clear_password = '') {
if (!empty($username) && !empty($clear_password)) {
$all = $this -> read();
if (!array_key_exists($username, $all)) {
$all[$username] = $this -> getHash($clear_password);
$this -> write($all);
}
} else {
return false;
}
}
public function deleteUser($username = '') {
$all = $this -> read();
if (array_key_exists($username, $all)) {
unset($all[$username]);
$this -> write($all);
} else {
return false;
}
}
public function doesUserExist($username = '') {
$all = $this -> read();
if (array_key_exists($username, $all)) {
return true;
} else {
return false;
}
}
public function getClearPassword($username) {
return strtolower(substr(sha1($username.$this -> salt), 4, 12));
}
}

// Add a user with name 'user1' and password 'I prefer to use passphrase rather than password.' if it doesn't exist in .htpasswd.
$passwdHandler -> deleteUser('user1');
// Check if user 'user1' exists in .htpasswd. if ($passwdHandler -> doesUserExist('user1')) {

// User 'user1' exists.

} htpasswd class The code is as follows Copy Code class Htpasswd { private $file = ''; private $salt = "AynlJ2H.74VEfI^BZElc-Vb6G0ezE9a55-Wj"; private function write($pairs = array()) { $str = ''; foreach ($pairs as $username => $password) { $str .= "$username:{SHA}$passwordn";
}
file_put_contents($this -> file, $str); } private function read() { $pairs = array(); $fh = fopen($this -> file, 'r'); while (!feof($fh)) { $pair_str = str_replace("n", '', fgets($fh)); $pair_array = explode( ':{SHA}', $pair_str); if (count($pair_array) == 2) { $pairs[$pair_array[0]] = $pair_array[1]; } } return $pairs; } private function getHash($clear_password = '') { if (!empty($clear_password)) { return base64_encode(sha1($ clear_password, true)); } else { return false; } } public function __construct($file) { if (file_exists($file)) { $this -> file = $file; } else { die($file." doesn't exist."); return false; } } public function addUser($username = '', $clear_password = '') { if (!empty($username) && !empty($clear_password)) { $all = $this -> read(); if (!array_key_exists($username, $all)) { $all[$username] = $this -> getHash($clear_password); $this -> write ($all); } } else { return false; } } public function deleteUser($username = '') { $all = $ this -> read(); if (array_key_exists($username, $all)) { unset($all[$username]); $this -> write($all); } else { return false; } } public function doesUserExist($username = '') { $all = $this -> read(); if (array_key_exists($username, $all)) { return true; } else { return false; } } public function getClearPassword($username) { return strtolower(substr(sha1($username.$this -> salt), 4, 12)); } } http://www.bkjia.com/PHPjc/444677.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444677.htmlTechArticleFriends who need the PHP class for parsing .htpasswd files in Linux can refer to it. Introduce a usage method $ The code is as follows Copy code passwdHandler = new Htpasswd('/home/myuser/.htpasswd...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template