Simple PHP Authentication HTTP


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

I thought I would share this simple username password PHP auth script. It leans on HTTP to secure a page.. not bulletproof but quick and painless.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // input user/pass below
  4. $username = "INSERT YOUR DESIRED USERNAME HERE";
  5. $password = "INSERT YOUR DESIRED PASSWORD HERE";
  6.  
  7. // password protect via http
  8. if(!(isset($_SERVER['PHP_AUTH_USER']) &&
  9. isset($_SERVER['PHP_AUTH_PW']) &&
  10. $_SERVER['PHP_AUTH_USER'] == $username &&
  11. $_SERVER['PHP_AUTH_PW'] == $password)){
  12. header('WWW-Authenticate: Basic realm="Secured Area"');
  13. header('Status: 401 Unauthorized');
  14. }else{
  15. ?>
  16.  
  17. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  18. <html xmlns="http://www.w3.org/1999/xhtml">
  19. <head>
  20. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  21. <title>Page Name</title>
  22. </head>
  23.  
  24. <body>
  25. <!-- // html -->
  26. </body>
  27. </html>
  28. <?php } ?>

URL: http://www.thatgrafix.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.