Use your left/right keys to browse tutorials
PHP HTTP Authentication

PHP HTTP Authentication

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on October 10, 2009

Occasionally you may wish to make certain pages of your site only viewable to a select few. you can do this by using PHPs built in HTTP Authentication. The code needs to go right at the top of your php page so don’t get ‘Headers Already Sent’ errors. You can see that we’ve specified the username and password in the variables at the top of the script you can change these to reflect your own username and password.

You could easily make this authentication more dynamic by checking a database for the username and password. We can get whatever the user typed into the dropdown box by specifying the following superglobals.

//Username: 
<?php echo $_SERVER['PHP_AUTH_USER'];?>
//Password: 
<?php echo $_SERVER['PHP_AUTH_PW'];?>

The Code

<?php 

$config['admin_username'] = "demo";
$config['admin_password'] = "demo";


if (!($_SERVER['PHP_AUTH_USER'] == $config['admin_username'] && $_SERVER['PHP_AUTH_PW'] == $config['admin_password'])) {
    header("WWW-Authenticate: Basic realm=\"Papermashup.com Demo Admin\"");
    header("HTTP/1.0 401 Unauthorized");
	echo 'This is what happens if you press cancel';
	?>
<?php
    exit;
}
// if the username and password match show the rest of the content

?>

Be careful when coding the HTTP header lines. In order to guarantee maximum compatibility with all browsers, the keyword “Basic” should be written with an uppercase “B”, the realm string must be enclosed in double (not single) quotes, and exactly one space should precede the 401 code in the HTTP/1.0 401 header line.

demo



More tutorials from Papermashup
Comments
12 discussions around PHP HTTP Authentication
Older Comments
  1. Erwin says:

    Where can i download the Demo code pls…

    Ty in advance

Older Comments




Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.