iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

August 7, 2008
Wish XML a happy birthday
About
 
August 7, 2008
Poll: How important is SEO to your overal website strategy?
About
 
August 7, 2008
How to Create a Search Feature with PHP and MySQL
WebReference.com
 
August 7, 2008
1 comment
.net
 
August 6, 2008
10 New SEO Reports
About
 
August 5, 2008
Array Creator
EarthWeb.com
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

PHP's Header Function 

  Views:    19251
  Votes:    6
by Chief Programmabilities 3/22/05 Rating: 

Synopsis:

PHP's header() function allows you to send arbitrary HTTP headers. Among other things, header() can be useful for authentication or page-level redirection.
Pages: 
The Article

Sending HTTP Headers

The HyperText Transfer Protocal (HTTP) is the language that Web servers and Web clients use to talk to each other over the Internet. PHP's header() can be used to send raw, arbitrary HTTP headers. You can use it to take advantage of any kind of header-controlled functionality. The syntax of  header() takes a single argument, which is the header to be sent.

Example: Redirection

One useful kind of HTTP header is "Location:", which can act as a redirector. Simply put a URL after the "Location:" string, and the browser will start over again with the new address instead. An example:

<?php
      if(( isset( $gender )) && ( $gender == "female" )) {
        header( "Location:
http://programmabilities.com/php/secret.php" );
        exit;
      }
?>

<html><head><title>the inclusive page</title></head>
<body>
<h3>welcome!</h3>
We welcome anyone to this page even men! Talk amongst yourselves.
</body></html>


If we simply enter the URL for this page (http://programmabilities.com/php/inclusive.php), we will see the rendering of the HTML at the bottom. Or else, if we include the right GET argument (http://programmabilities.com/php/inclusive.php?gender=female), we find ourselves redirected to a different page entirely. The new Web address then shows up in the "Location" or "Address" bar of your browser.

This kind of redirection can be useful when you want the structure of your site to conditionally "branch" without having to make the user explicitly choose different links.

Example: HTTP authentication

Another useful thing you can do with HTTP is ask the browser to ask the user for a username and password, via a pop-up window. This is done with the WWW-Authenticate header, as in the following example:

<?php
      $username = 'username';
      $password = 'password';
      if ( !isset( $PHP_AUTH_USER ) ) {
             Header( "WWW-Authenticate: Basic realm=\"PHP login\"" );
             Header( "HTTP/1.0 401 Unauthorized" );
             echo "Canceled by user\n";
            exit;
      } else {
            if ( ( $PHP_AUTH_USER == 'userame' ) && ( $PHP_AUTH_PW == 'password' ) )
                 print( "The realm is yours<br/>" );
           else
                print( "We don't need your kind<br/>" );
    }
?>


If we visit the script for the first time, we will get a pop-up window. Once the user enters the information into the pop-up box, the script is automatically called again with new variables $PHP_AUTH_USER (set to the username string entered), $PHP_AUTH_PASSWD (set to the password string entered), and $PHP_AUTH_TYPE. These variables will continue to be set by the browser on each request -- one verification of identity per session.

Conclusion

In addition to redirection and authentication, you can use header() to explicitly set the expiration and caching behavior of your page, or send return status codes that tell the client whether whatever is returned should be considered a success or not.


Chief Programmabilities
Programmabilities.com

Pages: 

Similar/related articles:


 
  Sponsors