问题
今天,我的下级代理联系我说,我们的平台,应用服务批量导入后,用户密码含有“0”的都不显示,例如密码是“07121239”但是平台只显示“7121239”,今天做了下排查,本文仅记录一下今天排查的经历
排查前端
在前端加了个调试后密码依然是含“0”的,那就说明问题应该在后端
排查后端
由于项目是原生php写的,部分代码,就直接通过[说明]跳过,更改之前的部分代码
<?php '''数据库连接语句''' $edit_id=$_POST['edit_id']; $openid=$_POST['openid']; $db=$_POST['db']; $sql = "SELECT * FROM $db where id='$edit_id' and daili_openid='$openid'"; $res =$conn->query($sql); $result=$res->fetch_assoc(); if (!$result) { die( json_encode( array( 'code' => 200, 'msg' => '查询失败,不存在记录', 'data' => '' ),480) ); } else{ die( json_encode( array( 'code' => 100, 'msg' => '查询成功', 'data' => $result ),480) ); } ?>
代码返回是一个json,但是数据返回的password是“123456”,于是我重新打印了sql查询的语句,发现数据是“0123456”,说明应该不是sql查询问题,往下排查除了“json_encode”方法我找不到其他问题了
{"code":100,"msg":"\u67e5\u8be2\u6210\u529f","data":{"id":"45","phone":"15**********66","password":"123456","openid":"13*********4","daili_openid":"13*********4","address":"\u7389\u6eaa\u5e02\u7ea2\u5854\u533a","longitude":"102.55156461759","latitude":"24.359801443673","vip_data":"2023-07-10 16:22:03","vip_info":"false"}}
bug修改
大致猜测是因为json_encode()将0过滤掉了,修改代码如下:
<?php '''数据库连接语句''' $edit_id=$_POST['edit_id']; $openid=$_POST['openid']; $db=$_POST['db']; $sql = "SELECT * FROM $db where id='$edit_id' and daili_openid='$openid'"; $res =$conn->query($sql); $result=$res->fetch_assoc(); if (!$result) { $response = array( 'code' => 200, 'msg' => '查询失败,不存在记录', 'data' => '' ); } else { $response = array( 'code' => 100, 'msg' => '查询成功', 'data' => $result ); } $jsonData = json_encode($response, true); // 输出 JSON 字符串 die($jsonData); ?>
看下修改后的结果:
password显示“0123456”
{"code":100,"msg":"\u67e5\u8be2\u6210\u529f","data":{"id":"45","phone":"15**********66","password":"0123456","openid":"13*********4","daili_openid":"13*********4","address":"\u7389\u6eaa\u5e02\u7ea2\u5854\u533a","longitude":"102.55156461759","latitude":"24.359801443673","vip_data":"2023-07-10 16:22:03","vip_info":"false"}}