Four methods for passing parameters between PHP pages

不言
Release: 2023-03-23 10:32:01
Original
1505 people have browsed it

This article is a detailed analysis and introduction of the four methods of parameter transfer between PHP pages. Friends in need can refer to it

We define two php files, page01.php and page02.php, and Find a way to pass the content in page01 to page02, and then let us continue to use it.
-------------------------------------------------- ---------------------------------
The first:
Use 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.

Copy code The code is as follows:

<?php 
       setcookie(&#39;mycookie&#39;,&#39;自灵&#39;);
?>
Copy after login

<br/>It’s that simple, we have created the cookie. <br/>We defined a variable mycookie, whose value is the string 'self-spirit'. <br/>We can name the cookie variable whatever we want and define multiple cookie variables. <br/>Accept cookies on page02. <br/>

Copy code The code is as follows:

<?php
     $wuziling = $_COOKIE[&#39;mycookie&#39;];
     echo $wuziling;
?>
Copy after login

We use $_COOKIE[] to extract the variable mycookie in the cookie and pay its value to $wuziling. Then simply output. <br/>Okay, here we are done using cookies to pass parameters from page to page. <br/>-------------------------------------------------- ----------------------------------<br/>Second type:<br/>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. <br/>Set a session in page01. <br/>

Copy code The code is as follows:

<?php 
session_start();
$_SESSION["temp"]=array(&#39;123&#39;,&#39;456&#39;,&#39;789&#39;);
?>
Copy after login

<br/>To use session, session must be started. session_start(); is the method to start the session. Generally it should be written first. <br/>In the second statement, I defined a $_SESSION["temp"] array. The name of the array is $_SESSION["temp"], which stores 3 strings. <br/>Accept session on page02. <br/>

Copy code The code is as follows:

<?php 
     session_start();
     for($i=0;$i<3;$i++)
     {
             echo $_SESSION[&#39;temp&#39;][$i].&#39;<br />&#39;;
     }
?>
Copy after login

<br/>First start the 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. <br/> Below we use a for loop to output its content. <br/>[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']<br/>[When we write $_SESSION["temp"], temp plus double quotes or single quotes are equivalent. 】<br/>【Here we define an array when defining session variables, or we can define ordinary variables, just like what is mentioned in cookies】<br/>----------------- -------------------------------------------------- -------------<br/>Third option:<br/>Use a form to pass. <br/>page01.php is written like this: <br/>

Copy code 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 attribute action in the form directly specifies this Which page the form content is passed to. method specifies the method of transfer. post stands for using messaging, just like how we send text messages. <br/>page02.php is written like this: <br/>

Copy code The code is as follows:

<br/>
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'];
[If you don’t understand something, please refer to another post in this section that details form submission]
[The value of method can also be get]
-------------------------------------------------- ----------------------------------
Fourth type:
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:

Copy code The code is as follows:



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:

Copy code The code is as follows:




##Use $_GET[ ] to get new The value can then be output or used for other purposes. At this time, the new variable and its value can be directly seen in the browser address bar.

Related recommendations:

Detailed explanation of php page static example

php page setting cache time example code

The above is the detailed content of Four methods for passing parameters between PHP pages. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!