资讯专栏INFORMATION COLUMN

PHP接收前端各种bug传值整理

wqj97 / 3600人阅读

摘要:接收前端传值各种情况整理服务端代码情况传结果传代码结果传结果传结果传个结果传结果传个结果传个加个非空对象结果传结果传结果传结果传结果传结果用抓包工具发现请求里面并不会发送无效的字段和,所以不是丢弃了,而是没收到当传的值是里的,会转换成

PHP接收前端传值各种情况整理 服务端代码:

</>复制代码

  1. header("Access-Control-Allow-Origin:*");
  2. var_dump($_POST);
  3. exit;
情况 1) 传null

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": null
  3. }, function(data, status) {
  4. console.log(data);
  5. });

结果:

</>复制代码

  1. array(1) {
  2. ["test"]=>
  3. string(0) ""
  4. }
2) 传""

代码:

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": ""
  3. }, function(data, status) {
  4. console.log(data);
  5. });

结果:

</>复制代码

  1. array(1) {
  2. ["test"]=>
  3. string(0) ""
  4. }
3) 传"[]"

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": "[]"
  3. }, function(data, status) {
  4. console.log(data);
  5. });

结果:

</>复制代码

  1. array(1) {
  2. ["test"]=>
  3. string(2) "[]"
  4. }
4) 传[]

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": []
  3. }, function(data, status) {
  4. console.log(data);
  5. });

结果:

</>复制代码

  1. array(0) {
  2. }
5) 传2个[]

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": [],
  3. "test2": []
  4. }, function(data, status) {
  5. console.log(data);
  6. });

结果:

</>复制代码

  1. array(0) {
  2. }
6) 传{}

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": {}
  3. }, function(data, status) {
  4. console.log(data);
  5. });

结果:

</>复制代码

  1. array(0) {
  2. }
7) 传2个{}

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": {},
  3. "test2": {}
  4. }, function(data, status) {
  5. console.log(data);
  6. });

结果:

</>复制代码

  1. array(0) {
  2. }
8) 传1个{}加1个非空对象

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": {},
  3. "test2": {"a": 1}
  4. }, function(data, status) {
  5. console.log(data);
  6. });

结果:

</>复制代码

  1. array(1) {
  2. ["test2"]=>
  3. array(1) {
  4. ["a"]=>
  5. string(1) "1"
  6. }
  7. }
9) 传[{}]

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": [{}]
  3. }, function(data, status) {
  4. console.log(data);
  5. });

结果:

</>复制代码

  1. array(0) {
  2. }
10) 传[[{}]]

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": [[{}]]
  3. }, function(data, status) {
  4. console.log(data);
  5. });

结果:

</>复制代码

  1. array(0) {
  2. }
11) 传"nil"

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": "nil"
  3. }, function(data, status) {
  4. console.log(data);
  5. });

结果:

</>复制代码

  1. array(1) {
  2. ["test"]=>
  3. string(3) "nil"
  4. }
12) 传0

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": 0
  3. }, function(data, status) {
  4. console.log(data);
  5. });

结果:

</>复制代码

  1. array(1) {
  2. ["test"]=>
  3. string(1) "0"
  4. }
13) 传"null"

</>复制代码

  1. $.post("http://xxxxx.xx/index.php", {
  2. "test": "null"
  3. }, function(data, status) {
  4. console.log(data);
  5. });

结果:

</>复制代码

  1. array(1) {
  2. ["test"]=>
  3. string(4) "null"
  4. }

用抓包工具发现

http请求里面并不会发送"无效的"字段——[]和{},所以不是PHP丢弃了,而是没收到;

当传的值是js里的null,会转换成空字符串,http请求里面是test=,所以PHP接收到的test是个空字符串;

http协议不能表示值是什么类型,所以PHP只能什么都当做string

总结:

PHP对于接收到的每一个值,会转换成字符串变量

PHP对于接收到的,之所有会接收不到是因为被一系列规则过滤掉了

以上结论是在jQ和PHP7之下验证的,其他环境不一定保证正确,之后可以试验使用CURL发送数据试试。

TODO:

[-] 用CURL发送POST测试

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/29744.html

相关文章

  • PHP接收前端各种bug传值整理

    摘要:接收前端传值各种情况整理服务端代码情况传结果传代码结果传结果传结果传个结果传结果传个结果传个加个非空对象结果传结果传结果传结果传结果传结果用抓包工具发现请求里面并不会发送无效的字段和,所以不是丢弃了,而是没收到当传的值是里的,会转换成 PHP接收前端传值各种情况整理 服务端代码: header(Access-Control-Allow-Origin:*); var_dump($_POS...

    CrazyCodes 评论0 收藏0
  • 前端面试知识点目录整理

    摘要:写在前面金三银四又到了一年一度的跳槽季相信大家都在准备自己面试笔记我也针对自己工作中所掌握或了解的一些东西做了一个目录总结方便自己复习详细内容会在之后一一对应地补充上去有些在我的个人主页笔记中也有相关记录这里暂且放一个我的面试知识点目录大家 写在前面: 金三银四, 又到了一年一度的跳槽季, 相信大家都在准备自己面试笔记, 我也针对自己工作中所掌握或了解的一些东西做了一个目录总结,方便自...

    xzavier 评论0 收藏0
  • 前端面试知识点目录整理

    摘要:写在前面金三银四又到了一年一度的跳槽季相信大家都在准备自己面试笔记我也针对自己工作中所掌握或了解的一些东西做了一个目录总结方便自己复习详细内容会在之后一一对应地补充上去有些在我的个人主页笔记中也有相关记录这里暂且放一个我的面试知识点目录大家 写在前面: 金三银四, 又到了一年一度的跳槽季, 相信大家都在准备自己面试笔记, 我也针对自己工作中所掌握或了解的一些东西做了一个目录总结,方便自...

    enda 评论0 收藏0
  • php开发过程中不怎么常见的问题

    日常开发中碰到就记一下, 如果有朋友愿意分享的 bug 可以在评论中讨论啊 url 当中的参数有 ×tamp=1234567890这样的字段会被转义成xtamp=1234567890 这个不仅存在于页面解析当中,当使用 curl 请求时拼接的参数有这种格式的也会发生转义解决方法有两个: 把 timestamp 这个参数放在 urlQuery 的最前面, ?timestamp=1234...

    April 评论0 收藏0
  • 前端知识归纳

    摘要:继承性子标签会继承父标签样式优先级行内样式选择器类选择器标签选择器通配符继承机制创建了的元素中,在垂直方向上的会发生重叠。 技能考察: 一、关于Html 1、html语义化标签的理解; 结构化的理解; 能否写出简洁的html结构; SEO优化 a、理解:根据内容的结构化(内容语义化),选择合适的标签(代码语义化)便于开发者阅读和写出更优雅的代码的同时 让浏览器的爬虫和...

    sixleaves 评论0 收藏0

发表评论

0条评论

wqj97

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<