Browsing: PHP Redirect Script

Their is a common question for PHP developers – How do i redirect with PHP script? So i have written a very useful method for achieving this task easily. This method has compiled a complete HTTP status code list along with default behavior of PHP redirection power, so it combines redirection url and HTTP status code (optionally) beautifully.

You can use this simple PHP script to redirect a user from the page they entered to a different web page. One reason you may want to do this is that the page they are trying to access no longer exists. Using this PHP redirect script, they can be seamlessly transferred to the new page without having to click a link to continue.

Features

  • Manage redirects in the scripts easily
  • Users are redirected quickly and seamlessly
  • When using the ‘Back’ button, the user is taken to the last viewed page, not the redirect page
  • Supports comprehensive list of HTTP status codes
  • Works on all browsers
  • Combines “Redirection URL” and “HTTP status code” professionally.

Requirements

  • PHP5 or higher

You will find a comprehensive tutorial, demo and source code in the download package, but as usual i will explain this tutorial online as well.

Include “chip_functions.php”

I have written chip_redirect method in “chip_functions.php” so you may,

  • Include “chip_functions.php” at the top of your page
  • OR – you can copy “chip_redirect” method in your functions file
/*
|-----------------
| Chip Functions
|------------------
*/

require_once("chip_functions.php");

Variable: $url (string)

You have to assign a valid redirection URL to this variable, Example can be

  • http://www.example.com/
  • http://www.example.com/abc.php
  • abc.php

Variable: $code (integer)

This is an optional variable which tells the server about the status of redirection. You may be interested to read a comprehensive HTTP status code list.

Method Call

Now it is time to call “chip_redirect” method to achieve redirection smoothly.

/*
|-----------------
| Redirect
| Call without HTTP Status Code
|------------------
*/

chip_redirect( "http://www.example.com/" );

/*
|-----------------
| Redirect
| Call with HTTP Status Code
|------------------
*/

chip_redirect( "http://www.example.com/", 403 );

Complete Tutorial – PHP Redirect Script

/*
|-----------------
| Chip Functions
|------------------
*/

require_once(LIB_FSROOT . "chip_functions.php");

/*
|-----------------
| Redirect
|------------------
*/

chip_redirect( $_POST['url'] );

/*
|-----------------
| Required: Exit
|------------------
*/

exit;

Screenshot

Online Demo

I have developed online demo of this password generator class. I hope you will enjoy.

Download Package – 3 Kb

Click here to download complete package of this script. Package will contain,

  • chip_functions.php – Main Redirect Method
  • index.php – Demos home page
  • style.css – Minimal style

1.0

This is the first version of PHP redirect script. It is released on December 15, 2010.

email