Detailed explanation of four methods of parameter transfer between PHP pages_PHP Tutorial
WBOY
Release: 2016-07-21 15:07:50
Original
819 people have browsed it
We 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.
Copy code The code is as follows:
setcookie('mycookie','self-spirit') ; ?>
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.
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.
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.
Copy code The code is as follows:
session_start(); for($i =0;$i<3;$i++)
Start the session first. 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 when we define session variables, we define arrays, 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: Copy the code
The code is as follows:
The attribute action within the form directly specifies which page this form content is transferred to. method specifies the method of transfer. post stands for using messaging, just like how we send text messages. page02.php is written like this:
Copy the code
The code is as follows: $wu = $_POST['wuziling'];
echo $wu; ?>
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] -------------------------------------------------- ---------------------------------- The 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:
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 the code The code is as follows:
echo $ _GET['new']; ?>
Use $_GET[ ] to get the value of new, which 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.
http://www.bkjia.com/PHPjc/327516.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327516.htmlTechArticleWe define two php files, page01.php and page02.php, and find a way to pass the content in page01 to page02 , and then for us to continue using. --------------------------------------------------. ..
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