phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP Security: HTTP Authentication

Tag: security, login, authentication, HTTP authentication, HTTP   Category: PHP Security
post: 27 Feb 2008 read: 684


PHP Security HTTP Authentication Step By Step Tutorial - Part 1:You possible have found a web page that you want to open, sudden peep out a dialog window asking for username and password. common example is early page at cpanel (control panel to manage the web server use web based). It use HTTP Authentication.

In protecting web page with HTTP authentication, you have to deliver two header. header WWW-AUTHENTICATE tell to browser that an username and password needed. The other header is the status, which should be HTTP/1.0 401 Unauthorized. Compare this to the usual header, HTTP/1.0 200 OK.

Example, create a file named "protectHTTP.php" within www\test\phpsecurity. Enter following code:


<?php
  // test for username/password
  if(($_SERVER['PHP_AUTH_USER'] == "mia") AND
    ($_SERVER['PHP_AUTH_PW'] == "secret"))
  {
    echo("successfully!<br>\n");
  }
  else
  {
    //Send headers to cause a browser to request
    //username and password from user
    header("WWW-Authenticate: " .
    "Basic realm=\"PHPEveryDay's Protected Area\"");
    
	header("HTTP/1.0 401 Unauthorized");

    //Show failure text, which browsers usually
    //show only after several failed attempts
    print("This page is protected by HTTP ");
  }
?>

Point your browser to http://localhost/test/phpsecurity/protecthttp.php. You will get like this:

HTTP Authentication

PHP creates the PHP_AUTH_USER and PHP_AUTH_PW elements of the _SERVER array automatically if the browser passes a username and password.




| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Joomla: Fast Road to Understand Component Programming
Chart: How to Build Cool Animation Real Time Chart
Email: Send Attachement Mail
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


624
posting