PHP表单提交数据之get,post实例详解
本文章来给大家介绍一个入门教程关于PHP表单提交数据之get,post实例,有需要了解的同学可进入参考参考。
1.什么是表单
所谓表单,说简单点就是一对form标签。即:
。2.表单的作用
表单的作用是采集客户端提交的数据,并把数据提交给服务端。
比如在登录一个网站时,你需要输入自己的用户名和密码才能登陆。又比如你注册游戏账号时,你需要填写自己的邮箱、密码、年龄等等。
这些操作就要通过表单提交给服务器,最后由服务器记录在数据库。(不是所有)
3.表单的组成
表单的两个标签必须是成对出现的:
。form标签有两个必填的属性 : action,method。
action的作用是指定要提交到服务器的地址,比如我要提交到本站的info.php文件,那么就写成
而post会把form的数据集,即ProductID=1这个键值对包装在请求的body中,发送给服务器,然后向服务器请求数据。对于:
2. get和post的区别
2.1 安全性
如果用get提交一个验证用户名和密码的form,一般认为是不安全的。因为用户名和密码将出现在URL上,进而出现在浏览器的历史记录中。显然,在对安全性有要求的情况下,应该使用post。
2.2 编码
HTML 4.01 specification指出,get只能向服务器发送ASCII字符,而post则可以发送整个ISO10646中的字符(如果同时指定enctype=”multipart/form-data”的话)。
注意get和post对应的enctype属性有区别。enctype有两个值,默认值为application/x-www-form-urlencoded,而另一个值multipart/form-data只能用于post。
2.3 提交的数据的长度
HTTP specification并没有对URL长度进行限制,但是IE将请求的URL长度限制为2083个字符,从而限制了get提交的数据长度。测试表明如果URL超出这个限制,提交form时IE不会有任何响应。其它浏览器则没有URL的长度限制,因此其它浏览器能通过get提交的数据长度仅受限于服务器的设置。
而对于post,因为提交的数据不在url中,所以通常可以简单地认为数据长度限制仅受限于服务器的设置。
2.4 缓存
由于一个get得到的结果直接对应到一个URI,所以get的结果页面有可能被浏览器缓存。而post一般则不能,参考5。
2.5 引用和SEO
出于和上面相同的原因,我们可以用一个URI引用一个get的结果页面,而post的结果则不能,所以必然不能被seo/seo.html" target="_blank">搜索引擎搜到。
3. 服务端的处理
在服务端的ASP.NET程序中,对于get,我们用Request.QueryString[control-name]来取得对应的=current-value;对于post,我们用Request.Form[control-name]。
我们也可以笼统地使用Request[control-name]。但这样做的效率不如前者。我们可以用下面的程序比较
代码如下 | 复制代码 |
Request.QueryString和Request的效率: <script>// <![CDATA[<br /> protected void Page_PreInit(object sender, EventArgs e)<br /> {<br /> if(Request["InputString"] != null)<br /> {<br /> int count = 1000000;<br /> DateTime start;<br /> DateTime end;<br /> string value = “”;<br /> start = DateTime.Now;<br /> for(int i = 0;i < count;i++)<br /> {<br /> value = Request.QueryString["InputString"];<br /> }<br /> end = DateTime.Now;<br /> double requestGet = (end – start).TotalSeconds;<br /> start = DateTime.Now;<br /> for(int i = 0;i < count;i++)<br /> {<br /> value = Request["InputString"];<br /> }<br /> end = DateTime.Now;<br /> double request = (end – start).TotalSeconds;<br /> compare.InnerHtml = requestGet.ToString() + ” / ” + request.ToString() + ” = ” + (requestGet / request).ToString();<br /> get.InnerHtml = value;<br /> }<br /> }<br /> // ]]></script> Request.QueryString / Request Get: Request.QueryString / Request: |
同样的办法我们可以比较Request.Form和Request。
最后得到的结果(Request.QueryString[control-name] / Request[control-name]和Request.Form[control-name] / Request[control-name])大多数时候是小于1的。因此,我们因该尽量用Request.QueryString或 Request.Form来代替Request。
4. 正确地使用get和post
W3C的官方建议是:当且仅当form是幂等(idempotent)的时候,使用get。幂等是一个数学上的术语,其定义是:对于一个函数f : D -> D,如果D中的所有x满足f (f x) = f x,那么这个函数是幂等的。HTTP specification(比如RFC 2616)中,将幂等解释为:多次相同请求产生的副作用,和一次请求的副作用相同。
打个比方,如果你提交一个form会从Google上查询一个关键词,那么我们可以认为这个form是幂等的,因为1次提交和10次提交的副作用是差不多的(10次查询可能会多消耗一些电能);如果你提交一个form是订购一个终极大黄蜂(Utimate bumblebee),那么这就不是幂等的:要是你不小心多提交了1次form的话,你可能会被老婆乱骂,你不小心又提交了10次的话,你可能就破产了——一次提交和多次提交的副作用明显不同,所以这不是幂等的。
所以,一般来说,如果提交这个请求纯粹只是从服务端获取数据而不进行其他操作,并且多次提交不会有明显的副作用,应该使用get。比如:
* 搜索引擎的查询:http://www.google.com/search?q=yandixin;
* 分页:ArticleList.asp?Page=1。
如果提交这个请求会产生其它操作和影响,就应该使用post。比如:
* 修改服务器上数据库中的数据;
* 发送一封邮件;
* 删除一个文件。
另一个要考虑的因素是安全性。见2.1。
5. 浏览器差异
* IE 6:URL长度限制为2083个字符;post之后,刷新页面不会自动重新post数据,会出现警告;
并且,在后退的过程中有可能出现“Page has Expired”(通常是向自己post,然后后退时):
微软的技术支持人员号称“this is not a bug or problem specified to the ASP.NET but a security feature of the IE Browser”,并且说“You can also inform your users of this”,实在是荒唐。另外,一篇KB也提到这个问题,说将Response.CacheControl设为”Public”即可,经测试仅在第一次后退时有效。
* IE 7:和IE 6相同;
* Firefox 2.0.0.11:刷新页面不会自动重新post数据,会出现警告;
* Opera 9.24:正常(自动post数据);
* Safari 3.0.4:post之后,刷新页面、前进、后退都不会自动重新post数据,会出现警告。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

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

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

In this chapter, we are going to learn the following topics related to routing ?

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

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

Validator can be created by adding the following two lines in the controller.
