Username and password generator for .htpasswd files


/ Published in: PHP
Save to your folder(s)

Created by me as a quick and secure way of creating a username and password for basic authentication via a .htpasswd file on an Apache server.

A live demo is available at http://www.chrishair.co.uk/supgha/

Thanks.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // - - - - - - - - - - - - - - - - - - -
  4. // Author: Christopher Hair
  5. // Website: www.chrishair.co.uk
  6. // - - - - - - - - - - - - - - - - - - -
  7.  
  8. if (isset($_POST['submit'])){
  9. $error = '';
  10. $username = trim(stripslashes($_POST['username']));
  11. $password = trim(stripslashes($_POST['password']));
  12.  
  13. if ($username == '' || preg_match('/\s/m', $username)) {
  14. $error .= "<p><strong>Invalid Username.</strong></p>\n";
  15. }
  16.  
  17. if ($password == '' || preg_match('/\s/m', $password)) {
  18. $error .= "<p><strong>Invalid Password.</strong></p>\n";
  19. }
  20.  
  21. if ($error == '') {
  22. echo "<p>Success</p>\n";
  23. echo "<h1>" . htmlspecialchars($username) . ":" . crypt($password) . "</h1>\n";
  24. } else {
  25. echo $error;
  26. }
  27. }
  28.  
  29. ?>

URL: http://www.chrishair.co.uk/supgha/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.