HTTP Authentication in PHP

Sharing is caring!

Sometimes you might wish to make certain pages of your site just perceptible to a chosen few. you can do this by utilizing Phps constructed within Http Authentication. The code ought to go right at the highest point of your php page so don’t get ‘headers Already Sent’ mistakes. You can see that we’ve specified the username and secret word in the variables at the highest point of the script you can change these to reflect your own particular username and watchword.

You could effortlessly make this verification more dynamic by checking a database for the username and watchword. We can get whatever the client sorted into the dropdown box by tagging the accompanying 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

?>

2 thoughts on “HTTP Authentication in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Got Project on mind? Let's ConnectContact Us

Secured By miniOrange