Article

Apache HTTP Authentication with PHP

Page: 1 2

Let’s look at some sample code for a page that may only be viewed if the user enters username “myuser” and password “mypass”:

<?php  
if ($PHP_AUTH_USER != “mysuser”  
   or $PHP_AUTH_PW != “mypass”):  
 // Bad or no username/password.  
 // Send HTTP 401 error to make the  
 // browser prompt the user.  
 header("WWW-Authenticate: " .  
        "Basic realm=\”Protected Page: " .  
        "Enter your username and password " .  
        "for access.\””);  
 header(“HTTP/1.0 401 Unauthorized”);  
 // Display message if user cancels dialog  
 ?>  
 <HTML>  
 <HEAD><TITLE>Authorization Failed</TITLE></HEAD>  
 <BODY>  
 <H1>Authorization Failed</H1>  
 <P>Without a valid username and password,  
    access to this page cannot be granted.  
    Please click ‘reload’ and enter a  
    username and password when prompted.  
 </P>  
 </BODY>  
 </HTML>  
<?php else: ?>  
 ...page contents here...  
<?php endif; ?>

As you can see, checking the username and password entered is as simple as checking the variables $PHP_AUTH_USER and $PHP_AUTH_PW. When an incorrect user/pass combination is detected, you respond with two HTTP headers (using the PHP header function):

WWW-Authenticate: Basic realm=”Prompt the user here.”  
HTTP/1.0 401 Unauthorized

The first line informs the Web browser that Basic authentication is to be used. This just means that authentication is to be done with a username and password. The realm option lets the browser know when a particular username/password should be used when navigating throughout a group of Web pages. All pages that should use the same username/password (thus saving the user from having to re-enter them for every page) should have the same realm specified. Since this string is displayed in the dialog prompting the user, it’s an ideal place to put a message (for example: “If you’re a new user, enter ‘guest’ for your username and leave the password blank.”). Note that the double quotes in this line must be escaped with backslashes to prevent them from interfering with the double quotes surrounding the string in your PHP code.

The second line is a standard HTTP response code that lets the browser know that the username/password entered (if any) was incorrect, and that the user should be prompted to (re)enter them.

To protect an entire site, you would typically use PHP’s include function to use the code that performs the username/password check in every file on your site that you want protected without having to retype said code on every page.

I recently used this technique on a site that I set up for a small group of people working on a project together. I issued a single username/password combination that gave them access to the registration page, where each of them would create a personal username/password combination. The registration page would store those combinations in a MySQL database (for more information on this, see my Building a Database-Driven Web Site article series). All the other pages on the site would then access that database to determine if a given username/password combination was allowed to access the site or not.

This and other creative possibilities for making your password protection system more flexible make HTTP Authentication using PHP an extremely handy tool to have in your arsenal.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: