Php

How To Redirect to another page Using PHP

This PHP tutorial helps to create a page redirection using PHP. You can use redirection for a page, redirect to a url, PHP 301 redirect etc. We provided you with multiple methods to redirect a web page with PHP.

There are two main types of redirects in PHP: header() redirects and meta tag redirects.

You can 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.

An HTML redirect differs from a PHP redirect in that HTML is client-side while PHP is server-facing. This important distinction enables a PHP redirect to offer faster and more secure travel from one page to another.

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 an optional parameter. This parameter is used to hold the replace parameter.
  • $http_response_code: This parameter hold the HTTP response code.

PHP Redirect To a Page

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

<?php
/* Redirect browser */header("Location: https://phpflow.com/");
 
/* Make sure that code below does not get executed when we redirect. */exit;
?>

In the above code, We have used the header() function to send a “Location” header to the browser, which tells it to redirect to the specified URL. The “exit” statement is used to prevent any additional output from being sent to the browser.

PHP 301 Redirect

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

Related Post
header("HTTP/1.1 301 Moved Permanently");
header ("Location: https://phpflow.com/");
exit;

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.

Also, you can use the header() function with ob_start() and ob_end_flush().T this should be first line of your page like this:

<?php 
ob_start();
header(....)
?>

JavaScript via PHP

A windows.location object is implemented in JavaScript and can be used to retrieve the current URL and route the browser to a different website.

<!DOCTYPE html>
<html>
  <head>
    <title>window.location function</title>
  </head>
  <body>
    <p id="demo"></p>
    <script>
      document.getElementById("demo").innerHTML = "URL: " + window.location.href + "</br>";
      document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "Hostname: " + window.location.hostname + "</br>";
      document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "Protocol: " + window.location.protocol + "</br>";
    </script>
  </body>
</html>

Other Methods

There are two main methods for doing this. To redirect from within the HTML section of your page.

Option 1: – Meta Tag Redirects Using <meta>

Meta tag redirects to a web page using an HTML meta tag. It instructs the browser to automatically redirect to a different page.

We can redirect to a page using <meta> tag. Here’s an example:

<meta http-equiv="refresh" content="0;url=newpage.php">

In the above example, We have set a refresh time of 0 seconds, which tells the browser to immediately redirect to the specified URL.

Option 2: – using Javascript

window.location.replace is a method in JavaScript that replaces the current page with a new one. Let’s redirect to the page using javascript.

window.location.replace("http://newpage.php/");

It is similar to the window.location.href method, but instead of adding a new entry to the browser’s history, it replaces the current entry. This means that the user cannot use the “back” button to return to the previous page.

It prevents the user from returning to the previous page, such as after a form submission or a login page.

Conclusions:

We learned many ways to redirect a web page. The header() redirects are generally preferred over meta tag redirects, as they are more reliable and provide better control over the redirect process. The header() redirects allow you to set additional headers and status codes, such as HTTP 301 or 302

View Comments

  • hii sir I have a question
    How to redirect a php with parameter

    I have a forget form like that

    header("Location: resetpass.php")->with($name, $email);

    and I want anyone click on create new password so it will redirect reset password page with name and email
    please give any solution

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories