douhui9192 2013-09-22 18:31
浏览 39

奇怪的PDO行为

After some hours I have to post this question even if the answer maybe obvious to someone else.

The problem is that I want to test for the tokens, but even when I hardcode this, I still get INVALID. And I know it has to be right, because I tested it in PHPADMIN directly. What's odd is that it always passes the first time (without being hardcoded), but after that it is useless? The tokens are retrieved from a cookie.

public function findTriplet($credential, $token, $persistentToken) {

    $token = "459078a3b05ce938ed58f9678ac78f1agcgfsewe4";
    $persistentToken = "24d317b742da89ddf5b8ed50993d0f3cgcgfsewe4";
    $credential ="34";
    $q = "SELECT IF(SHA1(?) = {$this->tokenColumn}, 1, -1) AS token_match " .
         "FROM {$this->tableName} WHERE {$this->credentialColumn} = ? " .
         "AND {$this->persistentTokenColumn} = SHA1(?) LIMIT 1 ";
    $query = $this->db->prepare($q);
    $query->execute(array($token, $credential, $persistentToken));
    $result = $query->fetchColumn();

    if (!$result) {
        return self::TRIPLET_NOT_FOUND;
    } else if ($result == 1) {
        return self::TRIPLET_FOUND;
    } else {
        return self::TRIPLET_INVALID; }
    }

EDIT

The limit clause always catches the first row it finds, therefore I always get a mismatch Now I have to fix this.

The solution was simple. Delete the entry that was just validated before inserting a new row with the newly generated token. The new row should contain the SAME persistenceToken you just validated against. REMEMBER, this will still be UNSECURE, so set a FLAG on the serverside that this was a cookielogin, and require a REAL LOGIN for handling important data.

  • 写回答

1条回答 默认 最新

  • duanbi8089 2013-09-22 18:55
    关注

    I think your if checks are in the wrong order:

        if(!$result) {  return self::TRIPLET_NOT_FOUND;}
        elseif ($result == 1) {     return self::TRIPLET_FOUND;}
        else {  return self::TRIPLET_INVALID;}
    

    In the SQL, 1 means found, -1 means not found, and anything else would be invalid. But in the PHP, a -1 would fall into the else clause, and return self::TRIPLET_INVALID, whereas an invalid result would fall into if(!$result) and return self::TRIPLET_NOT_FOUND.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大