search
HomeBackend DevelopmentPHP ProblemWhat are the ways to transfer data in php?

What are the ways to transfer data in php?

Oct 18, 2019 pm 02:29 PM
phptransfer data

What are the ways to transfer data in php?

How php transmits data:

1. Use the cookie of the client browser

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.

Example: We define two php files, page01.php and page02.php, and find a way to transfer the content in page01 to page02, and then continue to use it.

Set a cookie in page01.

<?php 
       setcookie(&#39;mycookie&#39;,&#39;自灵&#39;);
?>

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

We have defined a variable mycookie, whose value is the string 'self-spirit'.

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

Accept cookies on page02.

<?php
     $wuziling = $_COOKIE[&#39;mycookie&#39;];
     echo $wuziling;
?>

We use $_COOKIE[] to extract the variable mycookie in the cookie and pay its value to $wuziling. Then simply output.

Okay, here we have completed using cookies to transfer parameters from page to page.

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

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

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.

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

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.

Note:

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 cumbersome, the subscript of the array 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

3. Use a form to pass

page01.php is written like this:

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

The attribute action in the form directly specifies which page the content of this form 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:

<?php 
     $wu = $_POST[&#39;wuziling&#39;];
     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'];

Note: The value of method can also be get

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

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

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:

<?php
     echo   $_GET[&#39;new&#39;];
?>

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.

For more PHP related knowledge, please visit php中文网!

The above is the detailed content of What are the ways to transfer data in php?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.