Simple login PHP code for single user (securing a page)

Here is a good example of a simple php script that is going to secure a page. The page will request an username and a password in order to show the content.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
 
$username = "your_name";
$password = "your_password";
 
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || ($_SERVER['PHP_AUTH_USER'] != $username) || ($_SERVER['PHP_AUTH_PW'] != $password))
	{
		//The username / password are incorrect so send the authentication headers
		header('HTTP/1.1 401 Unathorized');
		header('WWW-Authenticate:Basic realm="Login"');
		exit('Sorry, the username or password are not correct');	
	}
 
 
 
?>

The browser will simply ask for the username and the password. It will also be able to remember them for further uses.

ADD A COMMENT: