Home  >  Article  >  Backend Development  >  Summary of Several Methods for Passing Parameter Values ​​in PHP Pages_PHP Tutorial

Summary of Several Methods for Passing Parameter Values ​​in PHP Pages_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:211289browse

php is a server scripting language, and it is also the most popular WEB development language. Let's talk about several methods of passing parameters between four pages commonly used in PHP development applications.

First type:
Uses client browser cookies. 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 Copy code
代码如下 复制代码
setcookie("VisitTimes",$VisitTimes,time()+31536000);
?>
​​ ​ setcookie("VisitTimes",$VisitTimes,time()+31536000);

?>

It’s that simple, we have created the cookie.

We define a variable mycookie, whose value is the string 'self'.

We can name the cookie variable whatever we want and define multiple cookie variables.

 代码如下 复制代码
$HTTP_COOKIE_VARS["VisitTimes"]?($VisitTimes ++):($VisitTimes = 1);

echo "欢迎你第 ".$VisitTimes.
  "
光临我的主页

n";
  ?>
Accept cookies on page02.

The code is as follows Copy code
$HTTP_COOKIE_VARS["VisitTimes"]?($VisitTimes ++):($VisitTimes = 1);


echo "Welcome ".$VisitTimes. " Visit my homepage
n";

?>

For more details, please check: http://www.bKjia.c0m/phper/php-gj/33355.htm
We use $_COOKIE[] to extract the variable mycookie in the cookie and pay its value to $wuziling. Then simply output.
Okay, here we are done using cookies to pass parameters from page to page.
 代码如下 复制代码
session_start();
$_SESSION["temp"]=array('123','456','789');
?>


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.
 代码如下 复制代码
session_start();
for($i=0;$i<3;$i++)
{
echo $_SESSION['temp'][$i].'
';
     }
?>
Set up a session in page01.
The code is as follows Copy code
session_start();<🎜> $_SESSION["temp"]=array('123','456','789');<🎜> ?>
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 the session on page02.
The code is as follows Copy code
Session_start();<🎜> for($i=0;$i<3;$i++)<🎜> {<🎜> echo $_SESSION['temp'][$i].'
'; } ?>

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 complicated, the subscript of the array 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 type:

Use a form to deliver.
_post can only receive data in PHP when it obtains the method="post" of the form, as shown in the following code

The code is as follows
 代码如下 复制代码


 
a.php页面

if( $_post )
{
echo $_post['cn'];
}
else
{
echo '没有获取到值';
}
?>

Copy code