Home  >  Article  >  php教程  >  PHP from getting started to giving up series-04.php value transfer and retention between pages

PHP from getting started to giving up series-04.php value transfer and retention between pages

WBOY
WBOYOriginal
2016-08-25 10:20:471482browse

php from entry to abandonment series-04.php value transfer and maintenance between pages

1. Directory structure

2. Pass values ​​between two pages

 To transfer a small amount of data between two pages, you can use get submission or post submission. The difference between the two will not be described in detail.

 1. get submission

Use get submission to transfer data, and modify the URL sent to the server in the link address as shown belowhttp://www.cnblogs.com/MarkRao/p/php01.html?gName=mark&gAge=26

 Of course, you can also set method="get" in the form, receive the data value submitted by get in php, and use the predefined $_GET variable

  Information sent from a form with the GET method is visible to anyone (will be displayed in the browser's address bar), and there is a limit on the amount of information sent.

<span style="color: #008080;"> 1</span> <html>
<span style="color: #008080;"> 2</span> <head>
<span style="color: #008080;"> 3</span> <meta charset="utf-8">
<span style="color: #008080;"> 4</span> <title>get提交值</title>
<span style="color: #008080;"> 5</span> </head>
<span style="color: #008080;"> 6</span> <body>
<span style="color: #008080;"> 7</span> 
<span style="color: #008080;"> 8</span> <form action="getPage.php" method="get">
<span style="color: #008080;"> 9</span> 名字: <input type="text" name="gName">
<span style="color: #008080;">10</span> 年龄: <input type="text" name="gAge">
<span style="color: #008080;">11</span> <input type="submit" value="提交">
<span style="color: #008080;">12</span> </form>
<span style="color: #008080;">13</span> 
<span style="color: #008080;">14</span> </body>
<span style="color: #008080;">15</span> </html>

 The "getPage.php" file can now collect form data via the $_GET variable (note that the names of the form fields automatically become keys in the $_GET array):

<span style="color: #008080;">1</span> 欢迎 <?php <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_GET</span>["gName"]; ?>!<br>
<span style="color: #008080;">2</span> 你的年龄是 <?php <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_GET</span>["gAge"]; ?>  岁。

 2. Post submission

Use post submission to transfer data, set method="post" in the form, receive the data value submitted by post in php, and use the predefined $_POST variable

  Information sent from a form with the POST method is invisible to anyone (will not be displayed in the browser's address bar), and there is no limit on the amount of information sent.

 Note: However, by default, the maximum amount of information sent by the POST method is 8 MB (can be changed by setting post_max_size in the php.ini file).

<span style="color: #008080;"> 1</span> <html>
<span style="color: #008080;"> 2</span> <head>
<span style="color: #008080;"> 3</span> <meta charset="utf-8">
<span style="color: #008080;"> 4</span> <title>post提交值</title>
<span style="color: #008080;"> 5</span> </head>
<span style="color: #008080;"> 6</span> <body>
<span style="color: #008080;"> 7</span> 
<span style="color: #008080;"> 8</span> <form action="postPage.php" method="post">
<span style="color: #008080;"> 9</span> 名字: <input type="text" name="pName">
<span style="color: #008080;">10</span> 年龄: <input type="text" name="pAge">
<span style="color: #008080;">11</span> <input type="submit" value="提交">
<span style="color: #008080;">12</span> </form>
<span style="color: #008080;">13</span> 
<span style="color: #008080;">14</span> </body>
<span style="color: #008080;">15</span> </html>

 The "postPage.php" file can now collect form data via the $_POST variable (note that the names of the form fields automatically become keys in the $_POST array):

<span style="color: #008080;">1</span> 欢迎 <?php <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_POST</span>["pName"]; ?>!<br>
<span style="color: #008080;">2</span> 你的年龄是 <?php <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_POST</span>["pAge"]; ?>  岁。

3. Maintain values ​​between multiple pages

 To maintain data between multiple pages, you can use session to save it or you can use cookies to save it. The difference between the two will not be described in detail.

 1. Session save data

 PHP session variables are used to store information about the user session (session), or to change the settings of the user session (session). Session variables store information for a single user and are available to all pages in the application. The working mechanism of Session is: Create a unique ID (UID) for each visitor and store variables based on this UID. The UID is stored in a cookie or passed through the URL.

 Before you store user information in a PHP session, you must first start the session.

 Note: The session_start() function must be located before the tag:

 The correct way to store and retrieve session variables is to use PHP’s $_SESSION variable:

<span style="color: #008080;"> 1</span> <?php <span style="color: #008080;">session_start</span><span style="color: #000000;">();
</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 存储 session 数据</span>
<span style="color: #008080;"> 3</span> <span style="color: #800080;">$_SESSION</span>['viewCount']=1<span style="color: #000000;">; 
</span><span style="color: #008080;"> 4</span> ?>
<span style="color: #008080;"> 5</span> 
<span style="color: #008080;"> 6</span> <html>
<span style="color: #008080;"> 7</span> <head>
<span style="color: #008080;"> 8</span> <meta charset="utf-8">
<span style="color: #008080;"> 9</span> <title>session保持数据值</title>
<span style="color: #008080;">10</span> </head>
<span style="color: #008080;">11</span> <body>
<span style="color: #008080;">12</span> 
<span style="color: #008080;">13</span> <?<span style="color: #000000;">php
</span><span style="color: #008080;">14</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 取出 session 数据</span>
<span style="color: #008080;">15</span> <span style="color: #0000ff;">echo</span> "浏览量:". <span style="color: #800080;">$_SESSION</span>['viewCount'<span style="color: #000000;">];
</span><span style="color: #008080;">16</span> ?>
<span style="color: #008080;">17</span> 
<span style="color: #008080;">18</span> </body>
<span style="color: #008080;">19</span> </html>

 2. Cookie saves data

 Cookies are often used to identify users. A cookie is a small file that the server leaves on the user's computer. Each time the same computer requests a page through the browser, the cookie will be sent to the computer. With PHP, you can create and retrieve cookie values.

setcookie() function is used to set cookies.

 Note: The setcookie() function must be located before the tag.

The syntax is as follows

<span style="color: #008080;">1</span> <span style="color: #008000;">//</span><span style="color: #008000;">name存储的键名
</span><span style="color: #008080;">2</span> <span style="color: #008000;">//value存储的键值
</span><span style="color: #008080;">3</span> <span style="color: #008000;">//expire存储的超时时间
</span><span style="color: #008080;">4</span> <span style="color: #008000;">//path存储的位置
</span><span style="color: #008080;">5</span> <span style="color: #008000;">//domain存储区别的域名</span>
<span style="color: #008080;">6</span> <span style="color: #008080;">setcookie</span>(name, value, expire, path, domain);

 The $_COOKIE variable is used to retrieve the value of the cookie.

<span style="color: #008080;">1</span> <?<span style="color: #000000;">php
</span><span style="color: #008080;">2</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 输出 cookie 值</span>
<span style="color: #008080;">3</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_COOKIE</span>["user"<span style="color: #000000;">];
</span><span style="color: #008080;">4</span> 
<span style="color: #008080;">5</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 查看所有 cookie</span>
<span style="color: #008080;">6</span> <span style="color: #008080;">print_r</span>(<span style="color: #800080;">$_COOKIE</span><span style="color: #000000;">);
</span><span style="color: #008080;">7</span> ?>


  When deleting a cookie, you should change the expiration date to a point in time in the past :

<span style="color: #008080;">1</span> <?<span style="color: #000000;">php
</span><span style="color: #008080;">2</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 设置 cookie 过期时间为过去 1 小时</span>
<span style="color: #008080;">3</span> <span style="color: #008080;">setcookie</span>("user", "", <span style="color: #008080;">time</span>()-3600<span style="color: #000000;">);
</span><span style="color: #008080;">4</span> ?>

 OK, that’s it!

Statement:
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