Posted By

jadedbat on 12/03/08


Tagged

php password username authentication


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

luman
robe
Scooter
lagrsen
vkolev
userpiv
cokacola
peteheaney


Simple PHP Authentication HTTP


 / Published in: PHP
 

URL: http://www.thatgrafix.com

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.

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

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Scooter on December 21, 2008

You can also extend this to multiple usernames and passwords by using an associated array.

$users = array("fred" => "fl1ntst0n3", "barney" => "rubbl3");

In the IF statement, you feed PHPAUTHUSER to the array as a key. If a value is retrieved, the user is valid and you can check the value against PHPAUTHPW.

Posted By: jadedbat on December 27, 2008

Good Call :)

You need to login to post a comment.