在数字前使用{%2b)而不是(+)有什么区别?

2fjabf4q  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(192)

我在一个教程的基础上开发了一个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使用的输入是正确的还是错误的。它会说顶部错误。
我问这个问题是因为我的安卓系统在

java.langillegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

所以我在考虑是什么导致了这个错误。这是你的密码 PHP API . 在教程中,讲师使用了get\u result,但我将其更改为bind\u 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);
}

我想确定当使用带有+x-x-x-号的寄存器时,这是否使我的api显示top错误,因为正如我所说的,它与%2bx-x-一起工作正常。
数字也在保存中 + 在数据库中。
当我在这个基础上提出建议的时候,事情就相反了 + 工作和 2%B 将为空$token=$db->gettoken(urlencode($userphone),$isservertoken);
谢谢

vnjpjtjt

vnjpjtjt1#

在你的第二个函数声明中 getToken ,变量 $token 如果 while 构造没有循环(正如您所声明的 $token 在线循环内)。
所以,可能的结果是 $token 只是在回来的那一刻不存在 $token (这就是你正在经历的)。换句话说,在您的案例中没有任何结果循环。
另外:在json中加号不是一个很好的匹配:https://stackoverflow.com/a/1374456/5682311
使用php的 urlencode 在将phonenumber(或任何值)解析为 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; 
}

相关问题