Get Website Learn Coding Tech Story

PHP Programming Examples

<?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.

Learn more about HTTP Authentication