- 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 exit()
- PHP exit() vs. break
- PHP isset()
- 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 mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- 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 Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- 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 headers. Most of the time, the header() function is used to redirect to another page or URL. For example:
<?php
if(isset($_SESSION['user']))
{
echo "Welcome to codescracker.com!";
// block of code to process further
}
else
{
header('Location: login.htm');
exit();
}
?>
In the above example, since the session variable user is not set, program flow goes to the "else" block and executes the header() function, which redirects to the login.htm page. Because I have already created a login.htm page in the current directory. Therefore, the output of the above PHP example is:
Since the page has redirected to login.htm in a fraction of a second, we are directly seeing the login.htm page.
PHP header() Syntax
The syntax of the 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 a previous similar header or add a new header of the same type.
Note: The responseCode parameter is used when we need to force the default HTTP response code to a particular one.
Note: The default value of the replace parameter is TRUE. And the TRUE indicates that the header replaces the previous.
Use PHP header() to Start the 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 the above PHP example using the 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 of the 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 another name on the client's computer, for example, suppose I have a file named class_details.pdf saved on my server. But I want to make this file available for download on the client's computer system with the 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 the 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 as the file saved on the server.
Use PHP header() to prevent page caching
We all know that PHP scripts are known for their dynamic content. And the dynamic content must not be cached, either by the browser (client browser) or by proxy caches between the server and browser (client browser). Therefore, we need to use some PHP code to prevent caching for some pages or parts 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 »