Home  >  Article  >  Backend Development  >  What are the differences between php page submission methods?

What are the differences between php page submission methods?

青灯夜游
青灯夜游Original
2023-01-07 15:47:113803browse

There are two ways to submit data on PHP pages: get and post. The differences are: 1. When submitting with get, you can see the parameters on the URL address, while when submitting with post, you cannot see the parameters on the address bar; 2. Get is not safe. , post is safe; 3. Get submission is to submit parameters one by one, and post submission is to submit all parameters together as a whole; 4. Get submission generally does not exceed 255 bytes, and the size of post submission depends on the server; 5. Get is very Flexible, parameters can be passed as long as there is a page jump, while post is not flexible.

What are the differences between php page submission methods?

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

In the php page, there are two ways to submit data :get mode and post mode.

<form method="post" action=""></form>
<form method="get" action=""></form>

The difference between get method and post method

1. Appearance

get Submit and you can see the parameters in the address

What are the differences between php page submission methods?

post Submit and you can see the parameters in the address bar

What are the differences between php page submission methods?

2. Security

get is not safe, post is safe

The data submitted by get can be seen in the url bar, but the data submitted by post is invisible, so post is safer .

3. Submission principle

get submission is to submit parameters one by one

post submission is to submit all parameters together as a whole

4. Submitted data size

Get submission generally does not exceed 255 bytes

The size of post submission depends on the server

// 在php.ini中,可以配置post提交的大小
post_max_size = 8M

5. Flexibility

get is very flexible. You can pass parameters as long as there is a page jump

post is not flexible. Post submission requires the participation of a form

1、 html跳转
   <a>跳转</a>

2、JS跳转
<script>
	location.href='index.php?name=tom&age=20';
	location.assign('index.php?name=tom&age=20');
	location.replace('index.php?name=tom&age=20');
</script>

3、PHP跳转
header('location:index.php?name=tom&age=22')

Summary:

##GETPOSTAppearanceYou can see the parameters and values ​​passed in the addressNo data can be seen in the address barSubmit data sizeSubmit a small amount of data, different browsers have different maximum values, IE is 255 charactersSubmit a large amount of data, you can set it by changing the php.ini configuration file The maximum value of post submission dataSecurityLowHighSubmission principleThe submitted data and the data are independent Convert the submitted data into XML format for submissionVery flexible Flexible, as long as there is a page jump, you can get and pass dataInflexible

服务器数据的三种方式

$_POST:数组类型,保存的POST提交的值
$_GET:数组类型,保存的GET提交的值
$_REQUEST:数组类型,保存的GET和POST提交的值

根据提交表单数据的方式不同,获取表单数据的方法也有所不同:get方式可以使用预定义变量$_GET来获取;post方式可以使用预定义变量$_POST来获取;如果不知道 form 表单通过哪种方式提交数据,就可以使用预定义变量$_REQUEST来获取,它两种方式的数据都可以获取。

下面就来一一了解:

1、使用预定义变量$_GET快速获取表单数据(form表单需要设置为method="get"

在程序的开发过程中,由于 GET 方法提交的数据是附加到 URL 上发送的,因此在 URL 的地址栏中将会显示“URL+用户传递的参数”类型的信息,如下所示:

http://url?name1=value1&name2=value2 ...
  • name1、name2 为表单元素的名称(有表单元素的name属性设置),value1、value2 为表单元素的值。url和表单元素之间用“?”隔开,而多个表单元素之间用“&”隔开,每个表单元素的格式都是“name=value”,固定不变。

我们添加一下user.html文件的表单看看URL 的地址栏

What are the differences between php page submission methods?

What are the differences between php page submission methods?

user.php文件中可以直接使用预定义变量$_GET来获取数据,$_GET 全局变量是一个关联数组,数组的键名为表单元素 name 的值,数组的值为对应表单的值。(注只要是 URL 中的参数都可以使用 $_GET 获取。)

<?php
var_dump($_GET);
?>

What are the differences between php page submission methods?

可以使用$_GET['键名']的方式来一一获取每个表单元素的值:

<?php
header("content-type:text/html;charset=utf-8");
echo "用户名为:".$_GET['user']."<br>生日为:".$_GET['bday'];
?>

What are the differences between php page submission methods?

2、使用预定义变量$_POST快速获取表单数据(form表单需要设置为method="post"

post方法不依赖于 URL,不会将传递的参数值显示在地址栏中。

$_POST 全局变量也是一个关联数组,数组的键名为表单元素 name 的值,数组的值为对应表单的值。

<?php
header("content-type:text/html;charset=utf-8");
echo "用户名为:".$_POST['user']."<br>生日为:".$_POST['bday'];
?>

What are the differences between php page submission methods?

3、使用预定义变量$_REQUEST快速获取表单数据

$_REQUEST 全局变量是一个包含了  $_POST、$_GET 和 $_COOKIE 的数组,数组结构与 $_POST 和 $_GET 类似。

<?php
header("content-type:text/html;charset=utf-8");
var_dump($_REQUEST);
echo "用户名为:".$_REQUEST['user']."<br>生日为:".$_REQUEST['bday'];
?>

What are the differences between php page submission methods?

小结: 

 1、在开发的时候,如果明确是post提交就使用$_POST获取,如果明确get提交就用$_GET获取 

 2、request获取效率低,尽可能不要使用,除非提交的类型不确定的情况下才使用。

推荐学习:《PHP视频教程


The above is the detailed content of What are the differences between php page submission methods?. 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