PHP Redirect Uses & Example

This PHP tutorial helps to create page redirection using PHP. You can use redirection for a page, redirect to a url, PHP 301 redirect etc.

You can do redirect a page using the PHP header method. We’ll discuss different scenarios to redirection in PHP. The header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or form PHP.

Redirect Using header() Method

The header() function is a PHP built-in function for sending raw HTTP headers to the client. This PHP method can be used for redirection from one page to another, redirecting to a specific URL.

Syntax:

header( $header, $replace, $http_response_code )

The parameters are:

  • $header: This parameter is used to hold the header string.
  • $replace: It is optional parameter.This parameter is used to hold the replace parameter.
  • $http_response_code: This parameter hold the HTTP response code.

Redirect To A Page in PHP

Let’s redirect to another page using php header() method.The simple code:

PHP 301 Redirect

We can also create 301 rediret using PHP header() method.You can achieve by following code:

After the header, You must use die() or exit(). If the die() or exit() functions are not placed after the header('Location:....'), the script may continue to run, resulting in unexpected exception will throw.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.