Home  >  Article  >  Backend Development  >  How to jump to a specified address in php

How to jump to a specified address in php

王林
王林Original
2021-06-24 15:26:145551browse

The method for php to jump to the specified address is to add the header function to the header of the php file and enter the specified address url to be jumped after the parameter Location, for example [header('Location: http: //www.xxx.cn');].

How to jump to a specified address in php

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

In php, we generally have the following three ways to implement address jump. Let’s take a look at the three specific methods below.

First method: header()

The main function of the header() function is to output the HTTP protocol header (header) to the browser.

Syntax:

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

The optional parameter replace specifies whether to replace the previous similar header or add a header of the same type. The default is to replace.
The second optional parameter http_response_code forces the HTTP corresponding code to the specified value. The Location type header in the header function is a special header call, often used to implement page jumps. Note: 1. There cannot be a space between location and ":", otherwise it will not jump.

Example:

<?php
header(&#39;Location: http://www.example.com/&#39;);
exit;
?>

Note:

  • There cannot be any output before the header is executed

  • ##location and : There can be no spaces between

  • The php code after the header will still be executed, so you need to pay attention to using exit;

The second method: meta tag

Meta tag is a tag in HTML that is responsible for providing document meta-information. Using this tag in a PHP program can also achieve page jumps. If http-equiv is defined as refresh, when the page is opened, it will jump to the corresponding page within a certain period of time based on the value specified by content.

< meta http-equiv="refresh"
content="1;url=http://
www.baidu.com">

The third way: javascript

By using windows.location.href='url'; the page automatically jumps to the new address

< ?php 
$url = "http://www.baidu.com"; 
echo "< script language=&#39;javascript&#39;
type=&#39;text/javascript&#39;>"; 
echo "window.location.href=&#39;$url&#39;"; 
echo "< /script>"; 
?>  

Related learning videos Share:

php video tutorial

The above is the detailed content of How to jump to a specified address in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn