php - 在数字前使用 {%2B) 而不是 (+) 有什么区别?
问题描述
我正在开发一个基于教程的 api,它从用户那里获取令牌以识别该用户是否是管理员。
所以这只是一个问题,这真的很重要吗
+1xxx-xxxx-xxx
或者
%2B1xxx-xxxx-xxx
当我测试我的 api 时,如果我在 isServerToken 中输入错误的数据,它会返回 null,如果我使用正确的数据,它会返回正确的值。但是如果我使用它+
而不是[%2B]
它会给我这个错误:
<br />
<b>Notice</b>: Undefined variable: token in <b>/h**e/m***/pu**tml/***/db_functions.php</b> on line <b>389</b><br />
null
我将在下面评论 389 行
无论我对 isServerToken 使用正确还是错误的输入。它会说顶级错误。
我问这个是因为我从我的 android 收到了关于
java.langillegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
所以我正在考虑导致此错误发生的所有可能性。这是PHP API
. 在教程中,讲师使用了 get_result,但我已将其更改为 bind_result,因为它不适用于我的在线主机。
这是代码:
// Instructer Code
public function getToken($phone,$isServerToken)
{
$stmt = $this->conn->prepare("SELECT * FROM `token` WHERE phone=? AND isServerToken=?") or die ($this->conn->error);
$stmt->bind_param("ss",$phone,$isServerToken);
$result = $stmt->execute();
$token = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $token;
}
// What i've changed to Bind
public function getToken($phone,$isServerToken)
{
$stmt = $this->conn->prepare("SELECT phone, token, isServerToken FROM `token` WHERE phone=? AND isServerToken=?") or die ($this->conn->error);
$stmt->bind_param("ss",$phone,$isServerToken);
$result = $stmt->execute();
$stmt->bind_result($arr['phone'], $arr['token'], $arr['isServerToken']);
while ($stmt->fetch())
{
$token[] = $arr;
}
$stmt->close();
return $token; => This is where `Undefined variable: token in` happen.
}
从这部分调用应用程序:
//Only instructor code, i didn't changed anything this part
if(isset($_POST['phone']) && isset($_POST['isServerToken']))
{
$userPhone = $_POST['phone'];
$isServerToken = $_POST['isServerToken'];
$token = $db->getToken($userPhone,$isServerToken);
echo json_encode($token);
}
else{
$response = "Required parameter (phone , isServerToken) is missing!";
echo json_encode($response);
}
我想确保当使用带有 +xxxx-xxxx-xxx 编号的寄存器时,这是否会使我的 api 说顶部错误,因为正如我所说,它与 %2Bxxxx-xxxx-xxx 一起工作正常。
数字也保存+
在数据库中。
当我根据建议对这个基础提出意见时,事情变得相反现在+
工作并且2%B
将是空的。$token = $db->getToken(urlencode($userPhone),$isServerToken);
谢谢
解决方案
在您的第二个函数声明中,如果没有循环构造(因为您在循环内在线声明),则永远不会创建getToken
变量。$token
while
$token
因此,可能的结果是该变量$token
在返回的那一刻不存在$token
(这就是您所经历的)。换句话说,在您的情况下没有任何结果被循环。
另外:加号在 JSON 中不是很好的匹配: https ://stackoverflow.com/a/1374456/5682311
urlencode
在将其解析为之前在电话号码(或任何值)上使用php的函数json_encode
,听起来像是解决方案(它将+
符号转换为%2B
)
编辑:
public function getToken($phone,$isServerToken)
{
$stmt = $this->conn->prepare("SELECT phone, token, isServerToken FROM `token` WHERE phone=? AND isServerToken=?") or die ($this->conn->error);
$stmt->bind_param("ss",$phone,$isServerToken);
$result = $stmt->execute();
$stmt->bind_result($arr['phone'], $arr['token'], $arr['isServerToken']);
$token = array(); // <- added this line, to be sure variable $token always exists
while ($stmt->fetch())
{
$token[] = array_map('urlencode', $arr ); // <- urlencode all values within $arr
}
$stmt->close();
return $token;
}
推荐阅读
- autohotkey - 如何在一系列输入后发送一个加法序列?
- sonarqube - Sonar Portfolio 未显示已配置的项目,消息组合没有项目,或关联项目都没有代码行
- pandas - 对于数组中的所有元素,将布尔值转换为零或一
- java - Java 中的每个类和包在 Android 中是否也可用?
- c++ - std::initializer_list 什么时候可以轻松构造?
- apache-kafka - Kafka - 阅读一包消息
- java - 从 BaseAdapter 的 getView 中获取方法中的值
- react-native - React Native Linking 'openUrl':如何选择默认浏览器?
- ubuntu - 导入的虚拟机生成 IP 失败
- android - 如何在 DialogFragment 被关闭时不保存它的滚动位置?