Advertisement
Guest User

Untitled

a guest
Jul 11th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. <?php
  2. if (!isset($_SERVER['PHP_AUTH_USER'])) {
  3. header('WWW-Authenticate: Basic realm="My Realm"');
  4. header('HTTP/1.0 401 Unauthorized');
  5. echo 'Text to send if user hits Cancel button';
  6. exit;
  7. } else {
  8. echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
  9. echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
  10. }
  11. ?>
  12.  
  13.  
  14.  
  15.  
  16. Example #2 Digest HTTP Authentication example
  17.  
  18.  
  19. This example shows you how to implement a simple Digest HTTP authentication script. For more information read the » RFC 2617.
  20.  
  21.  
  22.  
  23. <?php
  24. $realm = 'Restricted area';
  25.  
  26. //user => password
  27. $users = array('admin' => 'mypass', 'guest' => 'guest');
  28.  
  29.  
  30. if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
  31. header('HTTP/1.1 401 Unauthorized');
  32. header('WWW-Authenticate: Digest realm="'.$realm.
  33. '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
  34.  
  35. die('Text to send if user hits Cancel button');
  36. }
  37.  
  38.  
  39. // analyze the PHP_AUTH_DIGEST variable
  40. if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) ||
  41. !isset($users[$data['username']]))
  42. die('Wrong Credentials!');
  43.  
  44.  
  45. // generate the valid response
  46. $A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
  47. $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
  48. $valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
  49.  
  50. if ($data['response'] != $valid_response)
  51. die('Wrong Credentials!');
  52.  
  53. // ok, valid username & password
  54. echo 'You are logged in as: ' . $data['username'];
  55.  
  56.  
  57. // function to parse the http auth header
  58. function http_digest_parse($txt)
  59. {
  60. // protect against missing data
  61. $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
  62. $data = array();
  63. $keys = implode('|', array_keys($needed_parts));
  64.  
  65. preg_match_all('@(' . $keys . ')=(?:(['"])([^2]+?)2|([^s,]+))@', $txt, $matches, PREG_SET_ORDER);
  66.  
  67. foreach ($matches as $m) {
  68. $data[$m[1]] = $m[3] ? $m[3] : $m[4];
  69. unset($needed_parts[$m[1]]);
  70. }
  71.  
  72. return $needed_parts ? false : $data;
  73. }
  74. ?>
  75.  
  76.  
  77. How to create a password for a .htpasswd file using PHP
  78.  
  79.  
  80. A user emailed me and asked how to create a password for a .htpasswd file using PHP. It is actually very simple.
  81. Below is a PHP script that generates a password for .htpasswd from the clear text password stored in $clearTextPassword.
  82.  
  83. Please note: For Apache servers running on Windows you have to use the htpasswd program to generate passwords, or use the htpasswd generator.
  84.  
  85. <?php
  86. // Password to be encrypted for a .htpasswd file
  87. $clearTextPassword = 'some password';
  88.  
  89. // Encrypt password
  90. $password = crypt($clearTextPassword, base64_encode($clearTextPassword));
  91.  
  92. // Print encrypted password
  93. echo $password;
  94. ?>
  95.  
  96. How to use the code:
  97. 1.Copy the above the code and paste it into your favorite text editor (ie notepad).
  98. 2.Change “some password” to the password you want to encrypt.
  99. 3.Save the code in file called htpasswd.php.
  100. 4.Upload htpasswd.php to your webserver.
  101. 6.The outputted text is your encrypted password.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement