How to Use Command Header in Php

How to Use Command Header in Php. To send alternative headers to the browser, use the header command in php. This command must be given to the browser before anything else, or else an error notice will appear:

Warning: Cannot change header information since it has already been transmitted by (output began at)

We can define what the content of the page provided to the browser is and how the browser should handle it with this command.

The header command can be used in the following ways:

Send the browser to a different page.

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">header ('Location: http://phpro.ir/index.php')</font></font>

Using the following code, the browser will be redirected to the address we wrote in the url after 10 seconds.

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">header ('Refresh: 10; url = http: //www.example.org/');</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
print 'You will be redirected in 10 seconds';</font></font>

To download a file by the browser, we use the following code:

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">header ('Content-Type: application / octet-stream');</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
header ('Content-Disposition: attachment; filename = "example.zip"');</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
header ('Content-Transfer-Encoding: binary');</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
// load the file to send:</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
readfile ('example.zip');</font></font>

Page not found

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">header ('HTTP / 1.1 404 Not Found');</font></font>

illegal access

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">header ('HTTP / 1.1 403 Forbidden');</font></font>

Server error

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">header ('HTTP / 1.1 500 Internal Server Error');</font></font>

Famous and widely used MIME types
CSS

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> header ('Content-type: text / css');</font></font>

Javascript

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> header ('Content-type: text / javascript');</font></font>

JPEG

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> header ('Content-type: image / jpeg');</font></font>

JSON

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> header ('Content-type: application / json');</font></font>

PDF

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> header ('Content-type: application / pdf');</font></font>

RSS

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> header ('Content-Type: application / rss + xml; charset = utf-8');</font></font>

Plain text

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> header ('Content-type: text / plain');</font></font>

XML

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> header ('Content-type: text / xml');</font></font>

GIF

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> header ('Content-Type: image / gif');</font></font>

PNG

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> header ('Content-Type: image / png');</font></font>

The most common formats that can be used in the header command are listed in the link below.

%d bloggers like this: