HTTP authentication from PHP

Do you know you can implement http authentication using php code? HTTP authentications are those you see when  you open control panel or administrative backend website, prompting for username and password in html  authentication prompt.

auth

This can be done in php using following code:

<?php

if (!isset($_SERVER['PHP_AUTH_USER'])) {

header(‘WWW-Authenticate: Basic realm=”My Realm”‘);

header(‘HTTP/1.0 401 Unauthorized’);

echo ‘Text to send if user hits Cancel button’;

exit;

} else {

echo “<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>”;

echo “<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>”;

}

?>

Source: http://php.net/manual/en/features.http-auth.php

1 Comments , , , , , , , , , , ,

One Response to “HTTP authentication from PHP”

  1. Sarath D R May 6, 2010 at 5:59 pm #

    Hi It is very useful for me since i forgot to put admin authentication….now it became easy..

Leave a Reply

More in php (27 of 104 articles)


This is a quick note for myself to remember some tricks while we develop a Facebook application. If you ...