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

WBOY
Release: 2016-07-12 08:54:16
Original
821 people have browsed it

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...
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