Summary of how PHP header () function is used

Source: Internet
Author: User
Tags http authentication php code zip

The header is divided into three parts:

The first part is the HTTP protocol version (http-version);
The second part is state code (status);
The third part is the causal phrase (reason-phrase).

Header () Function usage Description:

First, the role:
~~~~~~~~~
PHP is only the HTTP protocol to the HTML document to the browser, tell the browser how to deal with this page, as for the transfer of the content you need to familiarize yourself with the HTTP protocol, and PHP has nothing to do, can refer to http://www.w3.org/Protocols/rfc2616/rfc2616.
The traditional header must contain one of the following three kinds of headers and can only appear once.
Location:xxxx:yyyy/zzzz
Content-type:xxxx/yyyy
status:nnn xxxxxx

Second, first to understand how the HTTP protocol works
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The HTTP protocol is based on the request/response paradigm. After a client establishes a connection to the server, it sends a request to the server in the form of a Uniform Resource Identifier, protocol version number, followed by MIME information including the request modifier, client information, and possible content. When the server receives a request, it responds with a response that is formatted as a status line that includes the protocol version number of the information, a successful or erroneous code, followed by MIME information including server information, entity information, and possible content.
It is divided into four processes, in the HTTP protocol, the server refers to the provision of HTTP services, the client refers to the browser you use or download tools and so on. When communicating, the client issues a request connection, the server establishes the connection, then the client sends an HTTP request (request), and the server returns the response information (Respond), which completes an HTTP operation.

Third, the HTTP protocol status code means
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1xx Retention
2XX indicates that the request was successfully received
3xx requesting customers to further refine requests for completion
4XX Customer Error
5XX Server Error

Cases

The code is as follows Copy Code

Fix 404 Pages: Use this header directive to resolve the 404 header generated by the URL rewrite
Header (' http/1.1 OK ');

Set 404 Header: Page not found
Header (' http/1.1 404 Not Found ');

Pages are permanently deleted and can be told seo/seo.html "target=" _blank "> Search engines update their URLs
Set moved Permanently header (Good for redrictions)
Use with location header
Header (' http/1.1 moved Permanently ');
Access restricted
Header (' http/1.1 403 Forbidden ');
Server error
Header (' http/1.1 Internal Server Error ');

Redirect to a new location
Redirect to a new location:
Header (' location:http://www.m-bang.com);

Redirect after a period of delay
Redrict with delay:
Header (' refresh:10; url=http://www.sina.com.cn ');
print ' You'll be redirected in seconds ';

Overwrite x-powered-by value
Override x-powered-by:php:
Header (' x-powered-by:php/4.4.0′);
Header (' x-powered-by:brain/0.6b ');

Content language (en = 中文版)
Content language (en = 中文版)
Header (' content-language:en ');

Last modification time (available at cache time)
Last modified (good for caching)
$time = time () –60; or Filemtime ($FN), etc
Header (' last-modified: ' Gmdate (' d, D M Y h:i:s ', $time). ' GMT ');

Tell the browser what to get is not yet updated
Header for telling the browser the content
did not get changed
Header (' http/1.1 304 not Modified ');

Set the length of the content (available when caching):
Set content Length (good for caching):
Header (' content-length:1234′);

Used to download files:
Headers for a download:
Header (' Content-type:application/octet-stream ');
Header (' content-disposition:attachment; filename= "Example.zip");
Header (' content-transfer-encoding:binary ');

Prohibit caching of the current document:
Load the file to Send:readfile (' example.zip ');
Disable caching of the current document:
Header (' Cache-control:no-cache, No-store, max-age=0, Must-revalidate ');
Header (' Expires:mon, June 1997 05:00:00 GMT ');
To set the content type:
Date in the Pastheader (' Pragma:no-cache ');
Set Content Type:
Header (' content-type:text/html; charset=iso-8859-1′);
Header (' content-type:text/html; charset=utf-8′);
Header (' Content-type:text/plain ');

Plain text file
Header (' Content-type:image/jpeg ');

JPG picture
Header (' Content-type:application/zip ');

ZIP file
Header (' content-type:application/pdf ');

PDF file
Header (' Content-type:audio/mpeg ');

Audio MPEG (MP3,...) file
Header (' Content-type:application/x-shockwave-flash ');

Displays the logon dialog box, which can be used for HTTP authentication
Flash animation//show Sign In box
Header (' http/1.1 401 Unauthorized ');
Header (' Www-authenticate:basic realm= "top Secret");
print ' Text that'll be displayed if the user hits cancel or ';
print ' Enters wrong login da
Ta ';
?>

Now the form is filled out, we can use AJAX to authenticate users at any time, friendly prompts, but in the user did not pay attention to Ajax-friendly hints, submitted the wrong form, jump back to the original page, but the information filled out is lost. To support page bounce, there are the following options:
1. Use the Session_cache_limiter method: Session_cache_limiter (' private,must-revalidate '); but it should be noted that Session_cache_limiter () method is only useful before the Session_Start () method is written.
2. Use header to set control Caching method: Header (' cache-control:private,must-revalidate ');


Summary of several issues to be noted in page jump

1, location and ":" No space between the number, or there will be errors.
2, can not have any output before using header.
3, after the header of the PHP code will also be executed.
The following is a comparison with the redirect Response.Redirect in asp:
Example 1:

The code is as follows Copy Code
Response.Redirect ". /test.asp "
Header ("Location:.") /test.php ");

The difference between the two:
The redirect function of an ASP can work after sending a header file to a customer.
Such as

The code is as follows Copy Code
<%response.redirect ". /test.asp "%>
</body>Check that the next code in PHP will report an error:
?
Header ("Location:.") /test.php ");
?>
</body>You can only do this:
?
Header ("Location:.") /test.php ");
?>

That is, the header function cannot send any data to the customer.
Example 2:
in ASP

The code is as follows Copy Code
<%
Response.Redirect ". /a.asp "
Response.Redirect ". /b.asp "
%>
</body>The result is a redirected a.asp file.
What about PHP?
?
Header ("Location:.") /a.php ");
Header ("Location:.") /b.php ");
?>

We found it redirected to b.php.
The following code is not executed after the redirect is executed in ASP.
and PHP continues to execute the following code after executing the header.
In this respect the header redirection in PHP is not as redirected as in ASP. Sometimes after we redirect, we can't execute the following code:
In general, we use

The code is as follows Copy Code
if (...)
Header ("...");
Else
{
...
}

But we can simply use the following methods:

The code is as follows Copy Code
if (...)
{Header ("..."); exit ();}

Also note that if you are encoding with Unicode (UTF-8), you also have problems, and you need to adjust the cache settings.

The code is as follows Copy Code
<[email=%@]% @LANGUAGE = "Vbscript[/email]" codepage= "936"%>
<%if request.servervariables ("server_name") = "S.111cn.net" Then
Response.Redirect "News/index.htm"
Else%>
<%end if%>
<script>
var url = location.href;
if (Url.indexof (' http://www.111cn.net/')!=-1) location.href= '/index/index.htm ';
if (Url.indexof (' http://www.zhutiy.com/')!=-1) location.href= '/index1/index.htm ';
if (Url.indexof (' http://www.111cn.net/')!=-1) location.href= '/cn/index.asp ';
if (Url.indexof (' http://www.baidu.com/')!=-1) location.href= '/cn/index.asp ';
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.