If you want to restrict the access of a certain page or directory to specific users, you can use PHP Basic Authentication Method to validate the user. The below example show you the usage of this method with database to validate the user.
- Code: Select all
if (!isset($_SERVER['PHP_AUTH_USER']))
{
header('WWW-Authenticate: Basic realm="Your Website Title"');
header('HTTP/1.0 401 Unauthorized');
print "Sorry, you don't have the rights to access this System.";
exit( );
}
else
{
$Username = $_SERVER['PHP_AUTH_USER'];
$Password = $_SERVER['PHP_AUTH_PW'];
// Here you cab query the database to validate the user
if (// User Not Found - Show the Username/Password Dialog again)
{
header('WWW-Authenticate: Basic realm="Your Website Title"');
header('HTTP/1.0 401 Unauthorized');
print "Sorry, you don't have the rights to access this System.";
exit( );
}
}