PHP class used to parse. htpasswd files-php Tutorial

Source: Internet
Author: User
PHP class used to parse. htpasswd files
. Htpasswd File example:

User1: {SHA} kGPaD671VNU0OU5lqLiN/h6Q6ac =
User2: {SHA} npMqPEX3kPQTo + x/+ ZckHDrIcQI =
User3: {SHA} q1Fh2LTUjjkncp11m0M9WUH5Zrw =

  1. Class Htpasswd {
  2. Private $ file = '';
  3. Private $ salt = 'aynlj2h. 74VEfI ^ BZElc-Vb6G0ezE9a55-Wj ';
  4. Private function write ($ pairs = array ()){
  5. $ Str = '';
  6. Foreach ($ pairs as $ username => $ password ){
  7. $ Str. = "$ username: {SHA} $ password \ n ";
  8. }
  9. File_put_contents ($ this-> file, $ str );
  10. }
  11. Private function read (){
  12. $ Pairs = array ();
  13. $ Fh = fopen ($ this-> file, 'r ');
  14. While (! Feof ($ fh )){
  15. $ Pair_str = str_replace ("\ n", '', fgets ($ fh ));
  16. $ Pair_array = explode (': {SHA}', $ pair_str );
  17. If (count ($ pair_array) = 2 ){
  18. $ Pairs [$ pair_array [0] = $ pair_array [1];
  19. }
  20. }
  21. Return $ pairs;
  22. }
  23. Private function getHash ($ clear_password = ''){
  24. If (! Empty ($ clear_password )){
  25. Return base64_encode (sha1 ($ clear_password, true ));
  26. } Else {
  27. Return false;
  28. }
  29. }
  30. Public function _ construct ($ file ){
  31. If (file_exists ($ file )){
  32. $ This-> file = $ file;
  33. } Else {
  34. Die ($ file. "doesn't exist .");
  35. Return false;
  36. }
  37. }
  38. Public function addUser ($ username = '', $ clear_password = ''){
  39. If (! Empty ($ username )&&! Empty ($ clear_password )){
  40. $ All = $ this-> read ();
  41. If (! Array_key_exists ($ username, $ all )){
  42. $ All [$ username] = $ this-> getHash ($ clear_password );
  43. $ This-> write ($ all );
  44. }
  45. } Else {
  46. Return false;
  47. }
  48. }
  49. Public function deleteUser ($ username = ''){
  50. $ All = $ this-> read ();
  51. If (array_key_exists ($ username, $ all )){
  52. Unset ($ all [$ username]);
  53. $ This-> write ($ all );
  54. } Else {
  55. Return false;
  56. }
  57. }
  58. Public function doesUserExist ($ username = ''){
  59. $ All = $ this-> read ();
  60. If (array_key_exists ($ username, $ all )){
  61. Return true;
  62. } Else {
  63. Return false;
  64. }
  65. }
  66. Public function getClearPassword ($ username ){
  67. Return strtolower (substr (sha1 ($ username. $ this-> salt), 4, 12 ));
  68. }
  69. }

  1. $ PasswdHandler = new Htpasswd ('/home/myuser/. htpasswd ');
  2. // Add a user with name 'user1' and password 'I prefer to use passphrase rather than password.' if it doesn' t exist in. htpasswd.
  3. $ PasswdHandler-> addUser ('user1', 'I prefer to use passphrase rather than password .');
  4. // Delete the user 'user1' if it exists in. htpasswd.
  5. $ PasswdHandler-> deleteUser ('user1 ');
  6. // Check if user 'user1' exists in. htpasswd.
  7. If ($ passwdHandler-> doesUserExist ('user1 ')){
  8. // User 'user1' exists.
  9. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.