Wed Jun 10 2020

HTTP Authentication

PHP Scripting953 views

File Name: php-http-authentication.php

<?php
/* Check for authenticate user */
if(!isset($_SERVER['PHP_AUTH_USER'])) {
	
	/* Open login form for HTTP authentication */
	header('WWW-Authenticate: Basic realm="Restricted Area...Please Authenticate"');
	
	/* Set HTTP header as unauthorized */
	header('HTTP/1.0 401 Unauthorized');
	
	/* Message shows on by hiting cancel button in login form */
	echo 'Your refused to authenticate.';
	exit;
}
else {

	/* Show authenticated username */
	echo "Username: ".$_SERVER['PHP_AUTH_USER']."<br />";
	
	/*  Shows authenticated password */
	echo "Passowrd: ".$_SERVER['PHP_AUTH_PW'];
}
?>



/* Output
Input:
Username: ABC
Password: 123

Output:
Username: ABC
Password: 123

*/
/* --------------------------------- */
/*
Your refused to authenticate.
*/

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.