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 = 'value1'; header('Location: http://example.com/page.php?key1=' . $key1); // 传递多个参数 $key1 = 'value1'; $key2 = 'value2'; header('Location: http://example.com/page.php?key1=' . $key1 . '&key2=' . $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 = 'value1'; $key2 = 'value2'; $data = array('key1' => $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 = 'value1'; $_SESSION['key1'] = $key1; header('Location: http://example.com/page.php'); // 接收SESSION参数 session_start(); $key1 = $_SESSION['key1']; ?>
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 = 'value1'; setcookie('key1', $key1); header('Location: http://example.com/page.php'); // 接收COOKIE参数 $key1 = $_COOKIE['key1']; ?>
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!