The header function is most commonly used for sending http classes instead of downloading.
Jump
It will execute the last one, but it is conditional, for example:
| The code is as follows: |
Copy code |
|
Header ('Location: http://www.111cn.net ");
Header ('Location: http://www.g.cn ');
Header ('Location: http://www.baidu.com ');
This will jump to Baidu
Header ('Location: http://www.111cn.net '); echo' fire network;
Header ('Location: http://www.g.cn ');
Header ('Location: http://www.baidu.com '); |
This will jump to google
Sending status
Outputs the status value to the browser, which is mainly used for access permission control.
| The code is as follows: |
Copy code |
|
<? Php
Header ('http/1.1 401 unauthorized ');
Header ('status: 401 unauthorized ');
?> |
For example, to restrict a user from accessing this page, you can set the status to 404, as shown below, so that the browser shows that the page does not exist.
| The code is as follows: |
Copy code |
|
<? Php
Header ('http/1.1 404 Not Found ');
Header ("status: 404 Not Found ");
?> |
Download
Example
| The code is as follows: |
Copy code |
|
<? Php
$ Filename = 'path + actual filename ';
// File type
Header ('content-type: application/pdf ');
// Download the display name
Header ('content-Disposition: attachment; filename = "keep file name "');
Readfile ("$ filename ");
Exit ();
?> |
The header function is converted accordingly,
| The code is as follows: |
Copy code |
Header ('content-type: application/octet-stream'); // The output type. Select the corresponding type based on the MIME table provided below.
Header ('content-Disposition: attachment; filename= ..rar "'); // download the display name
Readfile('file name .rar 'on the server ');// |
File to be downloaded, including the path
Common MIME types
. Doc application/msword
. Docx application/vnd.openxmlformats-officedocument.wordprocessingml.doc ument
. Rtf application/rtf
. Xls application/vnd. ms-excel application/x-excel
. Xlsx application/vnd. Openxmlformats-officedocument.spreadsheetml.sheet
. Ppt application/vnd. ms-powerpoint
. Pptx application/vnd. Openxmlformats-officedocument.presentationml.presentation
. Pps application/vnd. ms-powerpoint
. Ppsx application/vnd. Openxmlformats-officedocument.presentationml.slideshow
. Pdf application/pdf
. Swf application/x-shockwave-flash
. Dll application/x-msdownload
. Exe application/octet-stream
. Msi application/octet-stream
. Chm application/octet-stream
. Cab application/octet-stream
. Ocx application/octet-stream
. Rar application/octet-stream
. Tar application/x-tar
. Tgz application/x-compressed
. Zip application/x-zip-compressed
. Z application/x-compress
. Wav audio/wav
. Wma audio/x-ms-wma
. Wmv video/x-ms-wmv
. Mp3. mp2. mpe. mpeg. mpg audio/mpeg
. Rm application/vnd. rn-realmedia
. Mid. midi. rmi audio/mid
. Bmp image/bmp
. Gif image/gif
. Png image/png
. Tif. tiff image/tiff
. Jpe. jpeg. jpg image/jpeg
. Txt text/plain
. Xml text/xml
. Html text/html
. Css text/css
. Js text/javascript