HTTP Authentication in PHP

Posted by midhun pottmmal On Wednesday, 26 December 2012 0 comments

     The HTTP Authentication hooks in PHP are only available when it is running as an Apache module and is hence not available in the CGI version. In an Apache module PHP script, it is possible to use the header () function to send an "Authentication Required" message to the client browser causing it to pop up a Username/Password input window.

      Once the user has filled in a username and a password, the URL containing the PHP script will be called again with the predefined variables PHP_AUTH_USER, PHP_AUTH_PW, and AUTH_TYPEset to the user name, password and authentication type respectively. These predefined variables are found in the $_SERVER and $HTTP_SERVER_VARS arrays. Both "Basic" and "Digest" (since PHP 5.1.0) authentication methods are supported. See the header () function for more information.
download code
02  // test for username/password
03  if(($_SERVER['PHP_AUTH_USER'] == "root") AND
04    ($_SERVER['PHP_AUTH_PW'] == "password"))
05  {
06    echo("successfully!<br>\n");
07  }
08  else
09  {
10    //Send headers to cause a browser to request
11    //username and password from user
12    header("WWW-Authenticate: " .
13    "Basic realm=\"Scriptime's Protected Area\"");
15    header("HTTP/1.0 401 Unauthorized");
17    //Show failure text, which browsers usually
18    //show only after several failed attempts
19    print("This page is protected by HTTP ");
20  }

user name ="root"
password= "password"
out put
out put
 If you would like one then just put the following in your comment 


Post a Comment