PHP redirect page explained


Home - Tutorials - Basics

In this tutorial I will show you how to redirect pages in PHP. You can find all important aspects and code examples about PHP redirecting.

Tutorial info:


Name:PHP redirect page explained
Total steps:1
Category:Basics
Level:Beginner
Product:See complete product

Bookmark PHP redirect page explained



spacer

Step 1 - PHP redirect page


PHP redirect page explained


Time to time it can be useful to redirect your visitor automatically to an other web page. Fortunately redirecting pages using PHP is quite an easy task.

To make a redirection you can use the header() function. This function send a raw HTTP header to the browser. As result the browser will be redirected to the page defined in this new HTTP header. You only have to take care that header() must be called before any actual output is sent. It means you can not use and html tags, echo or print functions. Below is an example how to use redirection in PHP:

Code:
  1. header('Location:www.php.net');
  2. $f = fopen('demo.txt','w+');
  3. fwrite($f,'Test');
  4. fclose($f);
  5. ?>
  6.  
  7.  
PHP F1

If you run this code your browser will be display the php.net site. However don't forget that the code after the header function will be executed! So to save resources you should call a die() function after the redirection as you can see below:
Code:
  1. header('Location:www.php.net');
  2. die();
  3. $f = fopen('demo.txt','w+');
  4. fwrite($f,'Test');
  5. fclose($f);
  6. ?>
PHP F1

The only thing you have to do is to change the URL inside the header parameter.

However if you write the echo before the redirection you will get an error like this:

Warning
: Cannot modify header information - headers already sent by

To avoid this problem you can use PHP output buffering as follows:
Code:
  1. ob_start();
  2. echo "Test";
  3. header("Location: www.php.net");
  4. ob_flush();
  5. ?>
PHP F1







Tags: php redirect, redirecting pages, redirect, php, php redirect page

<Back - Tutorial Index Next - Step 2> -->
PHP redirect page explained - Table of contents
Step 1 - PHP redirect page


F1 Site Family
AJAX F1
CSS F1
Database F1
Flash F1
HTML F1
Java F1
JavaScript F1
PhotoShop F1
PHP F1
Scripts F1
Tutorial F1
Windows F1
 
spacer

Family tutorials
AJAX PHP
AJAX File Upload
AJAX Rating System
MySQL Create User
Mysqldump Example
MySQL Concat
JavaScript String
JavaScript Timeout
JavaScript Tooltip

Total time: 0.022

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.