用来解析 .htpasswd 文件的 PHP 类
Lepaskan: 2016-07-25 09:05:01
asal
797 orang telah melayarinya
.htpasswd 文件示例:
user1:{SHA}kGPaD671VNU0OU5lqLiN/h6Q6ac=
user2:{SHA}npMqPEX3kPQTo+x/+ZckHDrIcQI=
user3:{SHA}q1Fh2LTUjjkncp11m0M9WUH5Zrw=
- 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}$password\n";
- }
- 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));
- }
-
- }
复制代码
- $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.
- }
复制代码
|
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31
Topik-topik yang berkaitan
Lagi>