当前位置: 首页 > 后端技术 > PHP

PHP接收前端各种bug值传递排序

时间:2023-03-29 21:10:34 PHP

PHP接收前端值,整理服务器端代码各种情况:header('Access-Control-Allow-Origin:*');var_dump($_POST);exit;case1)passnull$.post('http://xxxxx.xx/index.php',{"test":null},function(data,status){console.log(data);});result:array(1){["test"]=>string(0)""}2)Pass''code:$.post('http://xxxxx.xx/index.php',{"test":''},function(data,status){console.log(data);});Result:array(1){["test"]=>string(0)""}3)pass'[]'$.post('http://xxxxx.xx/index.php',{"test":'[]'},function(data,status){console.log(data);});结果:array(1){["test"]=>string(2)"[]"}4)Pass[]$.post('http://xxxxx.xx/index.php',{"test":[]},function(data,status){console.log(data);});Result:array(0){}5)Pass2[]$.post('http://xxxxx.xx/index.php',{"test":[],"test2":[]},function(data,status){console.log(data);});结果:array(0){}6)pass{}$.post('http://xxxxx.xx/index.php',{"test":{}},function(data,status){console.log(数据);});结果:array(0){}7)pass2{}$.post('http://xxxxx.xx/index.php',{"test":{},"test2":{}},function(data,status){console.log(data);});result:array(0){}8)传递1{}加上1个非空对象$.post('http://xxxxx.xx/index.php',{"test":{},"test2":{"a":1}},function(data,status){console.log(data);});结果:array(1){["test2"]=>array(1){["a"]=>string(1)"1"}}9)Pass[{}]$.post('http://xxxxx.xx/index.php',{"test":[{}]},function(data,status){console.log(data);});结果:array(0){}10)通过[[{}]]$.post('http://xxxxx.xx/index.php',{"test":[[{}]]},function(数据,状态){console.log(数据);});结果:array(0){}11)pass'nil'$.post('http://xxxxx.x??x/index.php',{"test":'nil'},function(data,status){console.log(数据);});结果:array(1){["test"]=>string(3)"nil"}12)Pass0$.post('http://xxxxx.xx/index.php',{"test":0},function(data,status){console.log(data);});Result:array(1){["test"]=>string(1)"0"}13)pass'null'$.post('http://xxxxx.xx/index.php',{"test":'null'},function(data,status){console.log(data);});result:array(1){["test"]=>string(4)"null"}抓包工具发现http请求中不会发送"invalid"字段-[]和{},所以不是PHP丢弃了,而是没有收到;js中传入的值为null时,会转为空字符串,http请求中包含test=,所以PHP接收到的test为空字符串;http协议不能指明值是什么类型,所以PHP只能把所有东西都当作字符串系列规则。以上结论在jQ和PHP7下得到验证。其他环境不一定保证正确性。您可以稍后尝试使用CURL发送数据。尝试。TODO:[-]使用CURL发送POST测试