Four methods of parameter passing between PHP pages are detailed

  • 2020-06-12 08:44:46
  • OfStack

We define two php files, ES0en01.php and ES2en02.php, and find a way to pass the contents of page01 to page02 for us to continue using.
--------------------------------------------------------------------------------
1 species:
cookie using the client browser. cookie is easily understood as a temporary file, which you can think of as a repository where the browser records information as it browses.
Set 1 cookie in page01.

<?php 
       setcookie('mycookie',' Since the spirit ');
?>

That's it. We've created cookie.
We define a variable, mycookie, whose value is the string 'self'.
We can give the cookie variable any name we want, and we can define multiple cookie variables.
Accept cookie on page02 page.

<?php
     $wuziling = $_COOKIE['mycookie'];
     echo $wuziling;
?>

We use $_COOKIE[] to extract the variable mycookie in cookie and pay its value to $wuziling. And then the simple output.
Okay, so here we are, passing parameters from page to page using cookie.
--------------------------------------------------------------------------------
The second:
Use the server side session. It is easy to understand session. Unlike cookie, it is a temporary repository on the server side. session is often referred to as a session.
Set 1 session in page01.

<?php 
session_start();
$_SESSION["temp"]=array('123','456','789');
?>

To use session, you must start session. session_start (); That's how you start session. Generally write in the first place.
In the second statement, I define a $_SESSION["temp"] array, named $_SESSION["temp"], which stores three strings.
Accept session on page02.

<?php 
     session_start();
     for($i=0;$i<3;$i++)
     {
             echo $_SESSION['temp'][$i].'<br />';
     }
?>

Start the session first. The variables we defined in page01 are ready to use after startup, unlike cookie, without any other fetching operations.
Next we use the for loop to output its content.
[Don't think of $_SESSION['temp'][$i] as a 2-dimensional array. It's a 1-dimensional array. The name of the array is $_SESSION["temp"].
When we write $_SESSION["temp"], temp in double or single quotes is equivalent.
When we define session variables, we define arrays. We can also define ordinary variables, as shown in cookie.
--------------------------------------------------------------------------------
Third:
Use forms to deliver.
page01.php:

<form action="page02.php" method="post">
     <input type="text" name="wuziling" />
     <input type="submit" name="submit" value=" submit " />
</form>

The property action within the form directly specifies which page the form content is delivered to. method indicates the way of transmission. post stands for using messaging, just like we do with texting 1.
Es98EN02.ES99en

<?php 
     $wu = $_POST['wuziling'];
     echo $wu;
?>

Get the value of the passed variable with $_POST[]. The variable name wuziling is defined in the name attribute of the input tag of the form.
It is then passed to another variable, $wu. So we can print it out. Direct output, echo $_POST['wuziling'];
[If you don't understand, please refer to another post on form submission in detail in this block]
[method can also be get]
--------------------------------------------------------------------------------
4 types:
Use hyperlinks to pass parameters. A lot of what we do online is click on hyperlinks and jump from page to page. You can also pass parameters at the same time.
page01.ES122en

<?php 
$var = 'I love you !';
?>
<a href="<?php echo "page02.php?new=".$var ?>">get</a>

Define a variable, $var.
The href property of the hyperlink a indicates that you want to go to the page02 page. With a question mark and a self-defined variable new, the value of new is the $var we want to pass.
Es136EN02.ES137en

<?php
     echo   $_GET['new'];
?>

Use $_GET[] to get the value of new, which can then be output or used for other purposes.
The browser address bar will see the new variable and its value directly.

Related articles: