- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP header() Function
The PHP header() function is used, when we need to send raw HTTP header. Most of the time, the header() function is used to redirect to another page/URL. For example:
<?php if(isset($_SESSION['user'])) { echo "Welcome to fresherearth.com!"; // block of code, to process 'user' } else { header('Location: login.htm'); exit(); } ?>
In above example, since the session variable user is not set, therefore program flow goes to else block and executes the header() function, that redirects to the login.htm page. Because I have already created a login.htm page in the current directory. Therefore the output of above PHP example is:
Since the page has redirected to login.htm in fraction of seconds, therefore we are directly seeing the login.htm page.
PHP header() Syntax
The syntax of header() function in PHP, is:
header(header, replace, responseCode);
From all three parameters, only the header parameter is required. And the other two parameters are optional.
Note - The header parameter is used to specify the header to send.
Note - The replace parameter is used when we need to specify whether the header should replace previous similar header or should add new header of same type.
Note - The responseCode parameter is used when we need to force the default HTTP response code to the particular one.
Note - The default value of replace parameter is TRUE. And the TRUE indicates that the header replaces the previous.
Use PHP header() to Start Automatic Download of a File
The PHP header() function can also be used to start an automatic download of a file on the client's browser. For example:
<?php header('Content-Disposition: attachment; filename="myfile.pdf"'); ?>
The output of above PHP example on header() function, to start an automatic download, is shown in the snapshot given below:
You can also use the Content-Type to indicate the original media type resource. That is, the Content-Type representation (in header()) is used, when we need to indicate the original media type of the resource (prior to any content encoding applied to send). For example:
header('Content-Type: application/pdf');
If you want to download a file with other name on the client's computer. For example, let us suppose, I have a file named class_details.pdf saved on my server. But i want to make this file, to download on the client's computer system with name download.pdf. Therefore, here is the code to follow, for this purpose:
<?php header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="download.pdf"'); readfile('class_details.pdf'); ?>
Now a file named download.pdf will be downloaded on the client computer whose content will be same as the content of class_details.pdf, a file saved on my server. Most of the time, this is used by the developer, to avoid providing the same name of the file, saved on the server.
Use PHP header() to Prevent Page Caching
As we all knows that, PHP scripts known for its dynamic content. And the dynamic content must be not cached, either by the browser (client browser) or by proxy caches between server and browser (client browser). Therefore, we need to use some PHP code to prevent cache for some page/part of an application written in PHP:
<?php header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: no-cache"); ?>
Note - Use PHP getallheaders() function to get all HTTP headers.
« Previous Tutorial Next Tutorial »