Posted By

hairzo on 07/14/11


Tagged

basic php password username htaccess apache secure htpasswd authentication


Versions (?)

Username and password generator for .htpasswd files


 / Published in: PHP
 

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

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.

  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. ?>

Report this snippet  

You need to login to post a comment.