Home php教程 PHP源码 PHP文件之间传值的四种方式说明

PHP文件之间传值的四种方式说明

May 25, 2016 pm 05:13 PM
php

一. POST传值

post传值是用于html的

表单跳转的方法,很方便使用。例如:

<span style="font-size:18px;">
 <form action=&#39;&#39; method=&#39;&#39;>  
 <input type=&#39;text&#39; name=&#39;name1&#39;>  
 <input type=&#39;hidden&#39; name=&#39;name2&#39; value=&#39;value&#39;>  
 <input type=&#39;submit&#39; value=&#39;提交&#39;>  
 </form>  
 </html>
</span>
Copy after login

form中的action填入的是跳转页面的url路径,method填入post方法。form表单中的提交按钮按下后,就会把form中有name的内容都传到填入的url中,可以通过$_POST['name']获取,例如:

<span style="font-size:18px;">
<?php  
   $a=$_POST[&#39;name1&#39;];  
   $b=$_POST[&#39;name2&#39;];  
?>
</span>
Copy after login

这里有个很方便的小技巧,在input标签中把type选为'hidden'时,这个input标签会隐藏起来,不在页面显示,但这input标签在form中,并且有name值和value值,同样会跟随提交按钮传递过去,这种隐藏标签可以传递一些不想显示出来的内容。

二.GET传值

GET传值是通过跟随url传递的,在页面跳转时,跟着url跳转。常用于标签的使用。例如:

跳转进入xxx.php后,就能通过$_GET['id']获取传递的值。GET方法常用于URL的目的是删除或读取某个id的php文件。
三.SESSION传值
SESSION是全局变量的一种,经常用于用户登陆后保存用户id之类的常用数据。一旦保存到SESSION中,其他页面都可以通过SESSION获取,SESSION的使用要开启session:

四.COOKIE传值

使用客户端浏览器的cookie。cookie很容易理解,就是一个临时文件,可以把它看成一个储藏室,浏览器在浏览的过程中记录一些信息,就暂时存放在这里。
在page01中设置一个cookie。

就是这么简单,我们已经创建cookie完毕。
我们定义了一个变量mycookie,它的值是字符串'自灵'。
我们可以随便给cookie变量起名字,可以定义多个cookie变量。
在page02页面接受cookie。

我们使用$_COOKIE[]提取cookie中的变量mycookie,将它的值付给$wuziling。然后简单的输出。
好了,到这里使用cookie从页面之间传递参数完毕。

Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

See all articles