JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

Redirection in PHP

Author:JIYIK Last Updated:2025/04/13 Views:

header()We will demonstrate another way to redirect a page in PHP using the function by sending an HTTP header to the browser . This method uses the built-in header()function in PHP, which takes Locationas a parameter, and its value is the URL of the desired page.

We'll also look at a way to redirect a page to another page using a helper function that takes the URL and status code as parameters, and then calls header()the function inside that method.

We will show another way to redirect to another page using PHP's echointernal javascriptredirect, which uses PHP's window.locationto store the URL of the page to redirect.


Use the function in PHP header()to send the URL as an HTTP header to the browser

We can use header()the header function, which takes Locationas a parameter. LocationThe value of is the URL of the desired page, to which we need to redirect. Note that the header function should be written in the file above the HTML tags and text. The header function does not execute after the other data is sent to the browser header(). It should be the first line of code to be executed.

For example, http://facebook.comstore the URL in a variable redirect_page. Use header()the function and Locationspecify the variable in the function's redirect_page. Then, call die()the function.

In the example below, the URL of Facebook is stored redirect_pagein the variable. The variable is used in the title function. When you run the following script, the page will redirect to the Facebook homepage. The function will block the script from continuing header()after the function is executed die()to prevent unexpected behavior. Make sure to place the PHP file in your local web server so you can run it in your browser.

Sample code:

# php 7.x
<?php
$redirect_page = 'http://test.com';
header('Location:'  .$redirect_page);
die();
?>

Use Helper method in PHP header()to redirect to other pages through function with status code

We will use a helper function redirectto redirect pages to other pages.

Define a method redirect. Pass the URL and status code as parameters in the function. header()Write the function inside the method. Pass Locationthe URL and status code as header()parameters to the function. Then, call die()the function. The script header()dies after executing the function because it encounters die()the function. Outside the function definition, call the function providing the URL http://example.com/ as a parameter redirect. It calls the function with the URL as a parameter.

In the following example, status codes 301are used to permanently redirect to another page. This script redirects the current page to http://example.com/. Check out the MDN web documentation to learn 303more about status codes.

Sample code:

#php 7.x
<?php
function redirect($url, $statusCode = 301) {
    header('Location: '  . $url, $statusCode);
    die();
}
redirect('http://example.com/');
?>

Redirecting to another page using output buffering in JavaScript

We will use JavaScript in PHP to redirect a page to another page. In the following example, we will redirect the page to the Twitter homepage.

Type the tag script text/javascriptas to write some JavaScript code. Use window.locationto store the URL -
http://www.demo.com. Use to echoprint the entire scripttag.

This method is called buffered output. It is a faster way to redirect to another page. Please check the PHP official website to know more about buffered output.

Sample code:

#php 7.x
<?php
echo '<script type="text/javascript">
    window.location = "http://www.demo.com/"
    </script>';
?>

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial