search
HomeBackend DevelopmentPHP TutorialPhp header() function syntax and usage code_PHP tutorial

Syntax:

Copy code The code is as follows:

Void header(string $string[ ,bool $replace=true [, int $http_response_code)

Send the original HTTP header to the client
Note: The
Header function must be called before any actual output, regardless of It is a normal html tag, a blank line in the file, or it comes from php. That is, there cannot be any form of output before this function.
Parameter description:

Parameter Description
string Required. Specifies the header string to be sent.
replace is optional. Indicates whether this header replaces the previous header, or adds a second header.
Default is true (replacement). false (allow multiple headers of the same type).

http_response_code is optional. Forces the HTTP response code to the specified value. (Available in PHP 4 and above)
Common usage examples:

1. Sending server status code

Copy code The code is as follows:

header('HTTP/1.0 404 Not Found');

Common status codes:

Status Code Description
100-199 indicates that the request has been successfully received, and the client is required to continue submitting the next request to complete the entire processing process.
200-299 indicates that the request has been successfully received and the entire processing process has been completed. 200 is commonly used
300-399 The request is not completed. The client needs to further refine the request. For example, the requested resource has been moved to a new address. Commonly used are 302,304
400-499. The client request is incorrect. Commonly used are 404
500- 599 An error occurred on the server side, commonly used 500
For details, please see: HTTP&FTP corresponding prompt information

2. Page jump

Copy code The code is as follows:

// Jump now Redirect
header('Location: http://www.jb51.net/');

// Redirect after 3 seconds
header('refresh:3;url=http:/ /www.jb51.net');

3. Set browser cache

Force the browser not to cache!
[code]
//header('Expires:-1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache -Control:no-cache,must-revalidate');
header('pragma:no-cache');
header('Last-Modified: '. date('D, j M Y H:i:s T') );


Note:
The time after Expires must be in GMT format, such as: gmdate("M d Y H:i:s");
Cache-control common The values ​​include private, no-cache, max-age, must-revalidate, etc. The default is private. Its function is divided into the following situations according to different re-browsing methods.
Cache-directive Description
public All content will be cached
private content will only be cached in the private cache
no-cache All content will not be cached
no-store All content will be cached Will not be cached in cache or temporary Internet files
must-revalidation/proxy-revalidation If the cached content is invalid, the request must be sent to the server/proxy for revalidation
max-age=xxx (xxx is numeric ) The cached content will expire after xxx seconds. This option is only available in HTTP 1.1, and if used together with Last-Modified, it has a higher priority
Open a new window
If the value of cache-control is specified private, no-cache, must-revalidate, then the server will be accessed again when a new window is opened. And if the max-age value is specified, the server will not be accessed again within this value, such as:
Copy code The code is as follows:

header('cache-control:max-age=5');

Indicates that the server will not be accessed again within 5 seconds after accessing this webpage

Press Enter in the address bar
If the value is private or Must-revalidate, the server will only be accessed the first time, and will not be accessed in the future. If the value is no-cache, it will be accessed every time. If the value is max-age, it will not be accessed again before expiration.
Press the back button
If the value is private, must-revalidate, max-age, it will not be accessed again, and if it is no-cache, it will be accessed again every time
Press the refresh button
Regardless of the value, it will be accessed repeatedly. When the Cache-control value is specified as "no-cache", accessing this page will not leave a page backup in the Internet temporary article folder. In addition, caching can also be affected by specifying the "Expires" value. For example, if you specify the Expires value as a time that has long passed, then if you press Enter repeatedly in the address bar when accessing this website, the access will be repeated every time:
Expires: Fri, 31 Dec 1999 16:00:00 GMT
About "Last-Modified
When the browser requests a URL for the first time, the return status from the server will be 200, the content is the resource you requested, and there is a Last-Modified attribute to mark this file in the service The time when the end of the period was last modified, the format is similar to this:
Last-Modified: Fri, 12 May 2006 18:53:33 GMT
When the client requests this URL for the second time, according to the provisions of the HTTP protocol, the browser The server will send an If-Modified-Since header to the server, asking whether the file has been modified after this time:

If-Modified-Since: Fri, 12 May 2006 18:53:33 GMT
If the server-side resource has not changed, it will automatically return HTTP 304 (Not Changed.) status code with empty content, thus saving the amount of data transferred. When the server-side code changes or the server is restarted, the resource is reissued and the return is similar to the first request. This ensures that resources are not sent to the client repeatedly, and also ensures that when the server changes, the client can get the latest resources.

Last-Modified improves performance
Smart developers will use Last-Modified together with the http header of the ETags request, which can take advantage of the cache of the client (such as the browser). Because the server generates the Last-Modified/Etag tag first, the server can use it later to determine whether the page has been modified. Essentially, the client is asking the server to validate its (client-side) cache by passing this token back to the server.

The process is as follows:

The client requests a page (A)
The server returns page A and adds a Last-Modified to A
The client displays the page and caches the page together with Last-Modified
The client requests page A again and passes the Last-Modified returned by the server in the last request to the server
The server checks the Last-Modified and determines that the page has not been modified since the last client request, and directly Returns a 304 response and an empty response body.

3. File download
For example, download a pdf file

Copy the code The code is as follows:

// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
//Indicate it It is an attachment, name it and download it
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in 1.pdf
readfile( '1.pdf');

In fact, you can understand these better this way!
Copy code The code is as follows:

header('content-type:application/octet-sream');
Header('Accept-Ranges:bytes');

//Return by bytes
Header('content-disposition:attachement;filename="filename"');
Header(' Accept-length:'.$filesize);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825013.htmlTechArticle Syntax: Copy the code as follows: Void header(string $string[,bool $replace=true [, int $ http_response_code) When sending the original HTTP header to the client, please note: The Header function must be in...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft