search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the four methods of parameter transfer between PHP pages, detailed explanation of the four methods_PHP tutorial

Detailed explanation of the four methods of parameter transfer between PHP pages, detailed explanation of the four methods

      2016-04-16

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.

The code is as follows:

<?<span>php 
       </span><span>setcookie</span>('mycookie','自灵'<span>);
</span>?>

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.

The code is as follows:

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

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.

The code is as follows:

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

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.

The code is as follows:

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

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 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 we define an array when defining session variables, 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:

The code is as follows:

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

The action attribute in the form directly specifies which page the form content is transferred to. method specifies the method of delivery. post stands for using messaging, just like how we send text messages.
page02.php is written like this:

The code is as follows:

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

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'];
[The value of method can also be get]
-------------------------- -------------------------------------------------- ---
The fourth method:
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:

The code is as follows:

<?<span>php 
</span><span>$var</span> = 'I love you !'<span>;
</span>?>
<a href="<?php echo "page02.php?<span>new</span>=".<span>$var</span> ?>">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:

The code is as follows:

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

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1121222.htmlTechArticleDetailed explanation of the four methods of parameter transfer between PHP pages, detailed explanation of the four methods 2016-04-16 Definition page01.php and page02.php two php files, find a way to transfer the content in page01 to page02, and then provide...
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment