PHP Scripting

HTTP Authentication

learn php by example of http authentication

6/10/2020
0 views
php-http-authentication.phpPHP
<?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.
*/
PHPhttp authenticationPHP codePHP tutorial

Related Examples