Table of Contents
Detailed explanation of the four methods of parameter transfer between PHP pages, detailed explanation of the four methods
Home Backend Development PHP Tutorial 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_PHP tutorial

Jul 12, 2016 am 08:54 AM
data transmission

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>?>
Copy after login

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>?>
Copy after login

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>?>
Copy after login

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>?>
Copy after login

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>
Copy after login

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>?>
Copy after login

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>
Copy after login

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>?>
Copy after login

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1121222.htmlTechArticleDetailed explanation of the four methods of parameter transfer between PHP pages, detailed explanation of the four methods 2016-04-16 Definition page01.php and page02.php two php files, find a way to transfer the content in page01 to page02, and then provide...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to transfer all data between two iPhones Detailed explanation: How to migrate data from old phones How to transfer all data between two iPhones Detailed explanation: How to migrate data from old phones Mar 18, 2024 pm 06:31 PM

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 &quot;transfer all&quot; the data. This issue's article List several ways to &quot;transfer part of the data&quot;. 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.

React API Call Guide: How to interact and transfer data with the backend API React API Call Guide: How to interact and transfer data with the backend API Sep 26, 2023 am 10:19 AM

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

Using HTTPS for data transmission in Java API development Using HTTPS for data transmission in Java API development Jun 18, 2023 pm 10:43 PM

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

PHP trait DTO: a key tool for optimizing the data transfer process PHP trait DTO: a key tool for optimizing the data transfer process Oct 12, 2023 pm 03:10 PM

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

PHP trait DTO: a key tool for optimizing the data transfer process PHP trait DTO: a key tool for optimizing the data transfer process Oct 12, 2023 pm 02:33 PM

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

How do C++ functions implement data transmission in network programming? How do C++ functions implement data transmission in network programming? Apr 27, 2024 pm 05:06 PM

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.

Practice of peripheral device control and data transmission functions of C++ in embedded system development Practice of peripheral device control and data transmission functions of C++ in embedded system development Aug 25, 2023 pm 07:10 PM

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? PHP Communication: How to handle data transfer interruptions? Aug 19, 2023 pm 10:33 PM

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,

See all articles