Header () sets the browser cache and header syntax

Source: Internet
Author: User
Tags mysql tutorial php tutorial browser cache

Definition and usage
Header ()The function sends the original http header to the client.

It is important to realize that the header () function must be called before any actual output is sent (in php Tutorial 4 and later versions, you can use the output cache to solve this problem ):

<Html>

Instance 1

The code is as follows: Copy code
<? Php # script 2.7-view_tasks.php
// Connect to the database:
$ Dbc = @ mysql tutorial I _connect ('localhost', 'username', 'password', 'test') or die ('<p> cocould not connect to the database! </P> </body> // Get the latest dates as timestamps tutorial:
$ Q = 'SELECT unix_timestamp (max (date_added), unix_timestamp (max (date_completed) from tasks ';
$ R = mysqli_query ($ dbc, $ q );
List ($ max_a, $ max_c) = mysqli_fetch_array ($ r, mysqli_num );
// Determine the greater timestamp:
$ Max = ($ max_a> $ max_c )? $ Max_a: $ max_c;
// Create a cache interval in seconds:
$ Interval = 60*60*6; // 6 hours
// Send the header:
Header ("last-modified:". gmdate ('R', $ max ));
Header ("expires:". gmdate ("r", ($ max + $ interval )));
Header ("cache-control: max-age = $ interval ");
?>

Instance 2

The code is as follows: Copy code

<? Php
// Result error
// Output already exists before header () is called
Header ('Location: http://www.111cn.net /');
?> Syntax
Header (string, replace, http_response_code)

You are prompted to save a generated pdf file (the content-disposition header is used to provide a recommended file name and force the browser to display the save dialog box ):

The code is as follows: Copy code

<? Php
Header ("content-type: application/pdf ");

// The file will be called downloaded.pdf
Header ("content-disposition: attachment?filename='downloaded= '");

// The pdf source is in original.pdf
Readfile ("original.pdf ");
?>

<Html>
<Body>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.