Home  >  Article  >  Backend Development  >  How to use PHP to implement page jump with parameters? (code example)

How to use PHP to implement page jump with parameters? (code example)

PHPz
PHPzOriginal
2023-03-27 19:10:242174browse

In the process of developing web applications, it is often necessary to implement page jumps. One of the more common situations is that you need to implement a page jump with parameters, in which case you need to use PHP to write the corresponding code. This article will introduce how to use PHP to implement page jumps with parameters.

1. The GET method passes parameters

The GET method passes parameters through the URL. Its format is:

http://example.com/page.php?key1=value1&key2=value2

where the URL is the address of the page , key1 and key2 are parameter names, value1 and value2 are parameter values. The code for passing parameters in the GET method is as follows:

<?php
// 传递单个参数
$key1 = &#39;value1&#39;;
header(&#39;Location: http://example.com/page.php?key1=&#39; . $key1);

// 传递多个参数
$key1 = &#39;value1&#39;;
$key2 = &#39;value2&#39;;
header(&#39;Location: http://example.com/page.php?key1=&#39; . $key1 . &#39;&key2=&#39; . $key2);
?>

Among them, the header() function is used to send HTTP header information, where the Location parameter represents the page address to be jumped.

2. POST method passes parameters

The POST method passes parameters through the HTTP request body. Its format is:

key1=value1&key2=value2

Among them, key1 and key2 is the parameter name, value1 and value2 are parameter values. The code for passing parameters in the POST method is as follows:

<?php
// POST方法传递参数
$key1 = &#39;value1&#39;;
$key2 = &#39;value2&#39;;
$data = array(&#39;key1&#39; => $key1, 'key2' => $key2);
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);

$context  = stream_context_create($options);
$result = file_get_contents('http://example.com/page.php', false, $context);
?>

Among them, the $data array is used to store parameters, the http_build_query() function converts the parameters into URL-encoded strings, and the $options array is used to set HTTP header information and Request method, the stream_context_create() function is used to create an HTTP stream context, and the file_get_contents() function is used to send an HTTP request and return the page content.

3. SESSION method passes parameters

The SESSION method saves the parameters into SESSION, and then realizes the effect of passing parameters through page jumps. The code for passing parameters in the SESSION method is as follows:

<?php
// SESSION方法传递参数
session_start();
$key1 = &#39;value1&#39;;
$_SESSION[&#39;key1&#39;] = $key1;
header(&#39;Location: http://example.com/page.php&#39;);

// 接收SESSION参数
session_start();
$key1 = $_SESSION[&#39;key1&#39;];
?>

Among them, the session_start() function is used to start SESSION, and the $_SESSION super global variable is used to save SESSION data.

4. COOKIE method passes parameters

The COOKIE method saves the parameters into COOKIE, and then realizes the effect of passing parameters through page jumps. The code for passing parameters in the COOKIE method is as follows:

<?php
// COOKIE方法传递参数
$key1 = &#39;value1&#39;;
setcookie(&#39;key1&#39;, $key1);
header(&#39;Location: http://example.com/page.php&#39;);

// 接收COOKIE参数
$key1 = $_COOKIE[&#39;key1&#39;];
?>

Among them, the setcookie() function is used to set the COOKIE data, and the $_COOKIE super global variable is used to save the COOKIE data.

Summary

This article introduces how PHP implements page jumps with parameters, including the GET method, POST method, SESSION method and COOKIE method. These methods have their own advantages and disadvantages, and developers can choose the appropriate method according to their own needs.

The above is the detailed content of How to use PHP to implement page jump with parameters? (code example). 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