


Detailed explanation of the four methods of parameter transfer between PHP pages, detailed explanation of the four methods_PHP tutorial
Detailed explanation of the four methods of parameter transfer between PHP pages, detailed explanation of the four methods
2016-04-16
Define two php files, page01.php and page02.php, and find a way to transfer the content in page01 to page02, so that we can continue to use it.
------------------------------------------------- ----------------------------------
First type:
Uses client browser cookies. A cookie is easy to understand. It is a temporary file. You can think of it as a storage room. The browser records some information during the browsing process and temporarily stores it here.
Set a cookie in page01.
The code is as follows:
<?<span>php </span><span>setcookie</span>('mycookie','自灵'<span>); </span>?>
It’s that simple, we have created the cookie.
We defined a variable mycookie, whose value is the string 'self'.
We can name the cookie variable whatever we want and define multiple cookie variables.
Accept cookies on page02.
The code is as follows:
<?<span>php </span><span>$wuziling</span> = <span>$_COOKIE</span>['mycookie'<span>]; </span><span>echo</span> <span>$wuziling</span><span>; </span>?>
We use $_COOKIE[] to extract the variable mycookie in the cookie and pay its value to $wuziling. Then simply output.
Okay, here we use cookies to transfer parameters from page to page.
------------------------------------------------- ----------------------------------
Second type:
Use server-side session. Understanding session is very easy. The difference from a cookie is that it is a temporary storage on the server side. Session is often called a session.
Set a session in page01.
The code is as follows:
<?<span>php </span><span>session_start</span><span>(); </span><span>$_SESSION</span>["temp"]=<span>array</span>('123','456','789'<span>); </span>?>
To use session, session must be started. session_start(); is the method to start the session. Generally it should be written first.
In the second statement, I defined a $_SESSION["temp"] array. The name of the array is $_SESSION["temp"], which stores 3 strings.
Accept session on page02.
The code is as follows:
<?<span>php </span><span>session_start</span><span>(); </span><span>for</span>(<span>$i</span>=0;<span>$i</span><3;<span>$i</span>++<span>) { </span><span>echo</span> <span>$_SESSION</span>['temp'][<span>$i</span>].'<br />'<span>; } </span>?>
First start a session. After startup, the variables we defined in page01 are already available and do not require any other acquisition operations. This is different from cookies.
Below we use a for loop to output its content.
[Don’t think that $_SESSION['temp'][$i] is a two-dimensional array. It is a one-dimensional array. The name of the array is $_SESSION["temp"]. Although this name is more complicated, the bottom of the array The mark is 'temp']
[When we write $_SESSION["temp"], temp plus double quotes or single quotes are equivalent. ]
[Here we define an array when defining session variables, or we can define ordinary variables, just like what is mentioned in cookies]
----------------- -------------------------------------------------- -------------
Third method:
Use a form to pass.
page01.php is written like this:
The code is as follows:
<form action="page02.php" method="post"> <input type="text" name="wuziling" /> <input type="submit" name="submit" value="提交" /> </form>
The action attribute in the form directly specifies which page the form content is transferred to. method specifies the method of delivery. post stands for using messaging, just like how we send text messages.
page02.php is written like this:
The code is as follows:
<?<span>php </span><span>$wu</span> = <span>$_POST</span>['wuziling'<span>]; </span><span>echo</span> <span>$wu</span><span>; </span>?>
Use $_POST[] to get the passed variable value. This variable name wuziling is defined in the name attribute of the form's input tag.
Then pass it to another variable $wu. So we can output. Direct output is also possible, echo
$_POST['wuziling'];
[The value of method can also be get]
-------------------------- -------------------------------------------------- ---
The fourth method:
Use hyperlinks to pass parameters. Many of our online operations involve clicking on hyperlinks to jump between web pages. Parameters can also be passed while clicking.
page01.php is written like this:
The code is as follows:
<?<span>php </span><span>$var</span> = 'I love you !'<span>; </span>?> <a href="<?php echo "page02.php?<span>new</span>=".<span>$var</span> ?>">get</a>
Define a variable $var.
The href attribute of hyperlink a states that it will jump to the page02 page. Add a question mark after it and a self-defined variable new [this name will be used on the page02 page]. The value of new is the $var we want to pass.
page02.php is written like this:
The code is as follows:
<?<span>php </span><span>echo</span> <span>$_GET</span>['new'<span>]; </span>?>
Use $_GET[ ] to get the value of new , and then you can output or use it for other purposes.
At this time, the new variable and its value can be directly seen in the browser address bar.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When many friends change their Apple phones, they want to import all the data in the old phone to the new phone. In theory, it is completely feasible, but in practice, it is impossible to "transfer all" the data. This issue's article List several ways to "transfer part of the data". 1. iTunes is a pre-installed software on Apple mobile phones. It can be used to migrate all data in old mobile phones, but it needs to be used in conjunction with a computer. The migration can be completed by installing iTunes on the computer, then connecting the phone and computer via a data cable, using iTunes to back up the apps and data in the phone, and finally restoring the backup to the new Apple phone. 2. iCloudiCloud is Apple’s exclusive “cloud space” tool. You can log in to your old phone first.

ReactAPI Call Guide: How to interact with and transfer data to the backend API Overview: In modern web development, interacting with and transferring data to the backend API is a common need. React, as a popular front-end framework, provides some powerful tools and features to simplify this process. This article will introduce how to use React to call the backend API, including basic GET and POST requests, and provide specific code examples. Install the required dependencies: First, make sure Axi is installed in the project

With the development of science and technology, network communication has become one of the important tools for information transmission in modern society. But at the same time, information transmission on the network faces the risk of malicious attacks and theft, so security is particularly important. Based on this, the HTTPS protocol came into being. It is a protocol that adds SSL/TLS encryption to the HTTP protocol to ensure network transmission security. As a language widely used in network development, Java naturally provides a rich API to support the HTTPS protocol. This article will

PHPtraitDTO: A key tool for optimizing the data transmission process. Specific code examples are required. Introduction: During the development process, data transmission is a very common requirement, especially when data is transferred between different levels. In the process of transmitting this data, we often need to process, verify or convert the data to meet different business needs. In order to improve the readability and maintainability of the code, we can use PHPtraitDTO (DataTransferObject) to optimize

PHPtraitDTO: A key tool for optimizing the data transfer process, specific code examples are required Overview: In PHP development, data transfer is a very common task, such as passing data from the controller to the view, passing data from the interface to the front end, etc. However, in the process of transmitting data, the data often needs to be processed, converted and encapsulated, which may lead to code redundancy and difficulty in maintaining. To solve this problem, we can use PHPtraitDTO (DataTransfer

C++'s network data transfer functions include recv() and send(), which are used to receive and send data on the server side. The following steps demonstrate the process of using recv() and send() to create an echo server: 1. Create a socket; 2. Set the server address information; 3. Bind the socket to the server address; 4. Listen for connections; 5 .Accept the connection, receive data and send it back to the client in a loop; 6. Close the connection and socket.

Practical introduction to peripheral device control and data transmission functions of C++ in embedded system development: As a technology with a wide range of applications, embedded systems are widely used in many fields, such as automobiles, home appliances, medical equipment, etc. In embedded system development, peripheral device control and data transmission are a very important function. This article will introduce how to use C++ language to implement the control and data transmission functions of peripheral devices, and provide practical code examples. 1. C++ Peripheral Device Control Function Practice In embedded systems, peripheral device control refers to the

PHP Communication: How to handle data transfer interruptions? IntroductionDuring the process of data transmission, data transmission may be interrupted due to network instability or other reasons. For developers, how to handle data transfer interruptions is a very important issue. This article will introduce how to handle data transmission interruption in PHP and provide relevant code examples. Methods for handling data transmission interruptions: Setting the timeout period When performing data transmission, you can set an appropriate timeout period. If the data transmission is not completed within the timeout period,
