Application of PHP header function

Source: Internet
Author: User
Use of the PHP header function

The cache of the Web page is controlled by the "Cache-control" in the HTTP message header, and the common values are private, No-cache, Max-age, must-revalidate, etc., and the default is private. Its function is divided into the following situations according to different ways of re-browsing:


(1) Open a new window

?The value is private, No-cache, Must-revalidate, and the server is re-accessed when a new window is opened.

If you specify a max-age value, the server is not re-accessed in the time within this value, for example:

Cache-control:max-age=5 (indicates that access will not go to the server within 5 seconds of accessing this page)


(2) Enter in the Address bar

?The value is private or must-revalidate the server is accessed only on the first visit and is no longer accessed at a later time.

?The value is No-cache, which is accessed every time.

?The value of max-age is not repeated until it expires.


(3) Press Back button

?? The value is private, must-revalidate, Max-age, and is not re-accessed.

?? A value of No-cache, which is repeatedly accessed every time


(4) Press the Refresh button

No matter what the value, it will be accessed repeatedly


When the Cache-control value is "No-cache", accessing this page does not leave a page backup in the Temporary Internet Article folder.


Also, the cache is affected by specifying a value of "Expires". For example, specifying the Expires value is a time long past, then when you visit this network repeatedly press ENTER in the Address bar, then each time will be repeated access: Expires:fri, Dec 1999 16:00:00 GMT


For example: Prevent pages from being cached in IE


HTTP response message Header settings:


CacheControl = No-cache

Pragma=no-cache

Expires =-1


Expires is a good thing, if the Web page on the server changes frequently, it is set to 1, indicating immediate expiration. If a webpage is updated daily 1 o'clock in the morning, you can set expires to 1 o'clock in the morning the next day.


When the HTTP1.1 server specifies CacheControl = No-cache, the browser does not cache the page.


Legacy HTTP 1.0 Servers cannot use the Cache-control header.

So for backwards compatibility with HTTP 1.0 servers, IE uses the Pragma:no-cache header to provide special support for HTTP.

If the client communicates with the server through a secure connection (https://), and the server returns the Pragma:no-cache header in the response,

Internet Explorer does not cache this response. Note: Pragma:no-cache prevents caching only when used in a secure connection, and if used in a non-secure page, is processed in the same way as expires:-1, the page is cached but is marked for immediate expiration

Header Common Directives

The header is divided into three parts:

The first part is the version of the HTTP Protocol (http-version);

The second part is state code (status);

The third part is the reason phrase (reason-phrase).


Fix 404 Pages:? Use this header command 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 tell the search engine to update their URLs

Set Moved permanently header (good for redrictions)??

Use with the location header??

Header (' http/1.1 301 Moved permanently ');?


Restricted access

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.sina.com.cn);??

??

Redirect after a period of delay

Redrict with delay:??

Header (' refresh:10; url=http://www.sina.com.cn ');??

print ' You'll be redirected in ten 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 modified time (can be used when caching)

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 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 (can be used when caching):

Set content Length (good for caching):??

Header (' content-length:1234 ');??

??

To download files:

Headers for an download:??

Header (' Content-type:application/octet-stream ');??

Header (' content-disposition:attachment; filename= ' example.zip ');??

Header (' content-transfer-encoding:binary ');??

??

Disable 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, Jul 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 Login 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 data ';? >

  • 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.