Skip to content


Basic User Authentication using Database

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

Basic User Authentication using Database

Postby php developer » May 26th, 2009, 5:24 am

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( );
   }
}
User avatar
php developer
 
Posts: 53
Joined: July 25th, 2008, 4:56 am

Return to Board index

Return to PHP / MySQL / XML

Who is online

Users browsing this forum: Google [Bot] and 0 guests