drjyvoi734793 2015-06-29 14:01
浏览 67
已采纳

PHP中相同的字符串不匹配[重复]

This question already has an answer here:

I'm trying to compare two strings (one from my database and another supplied by the user) and see if they match! The problem I'm having is that they don't seem to match - even though the strings seems to be exactly identical?

My PHP code is below:

public function Verify($pdo, $id, $token) {
    $prepsql = $pdo->prepare("SELECT * FROM Profiles WHERE id = '$id' LIMIT 1");
    $prepsql->execute();
    $currentrow = $prepsql->fetch();
    $current = preg_replace("/[^a-zA-Z0-9]+/", "", $currentrow["token"]);
    echo '<p>'.var_dump($current).'</p>';
    echo '<p>'.var_dump($token).'</p>';
    $token = preg_replace("/[^a-zA-Z0-9]+/", "", $token);
    if ($current == null || $current = "") {
        return false;
    } else {
        if (strcmp($token, $current) == 0) {
            return true;
        } else {
            return false;
        }
    }
}

And here is the webpage output:

  string(244) "CAAW4HRuZBuB4BACA7GffOAwLHgmLgMMLGQxDAw8IJDCwahZAh0S4wZAcP8Q9DmMwsDpBq7jFcH1EzUIsZBbhKov12utoYFQns0HhgB5xKLeDqtZBRqavaNjNSn7KAcObZAEcavQCRbGlVKZBArfDEHskBSR8qAoU543DVTZCOyHm5oYNDVafwHl0bAkc4jyIhh2YHEPaNpWGC0FhezsSidOgLjnfFq8CeLVxHH0nUZBMLgAZDZD"
<p></p>string(244) "CAAW4HRuZBuB4BACA7GffOAwLHgmLgMMLGQxDAw8IJDCwahZAh0S4wZAcP8Q9DmMwsDpBq7jFcH1EzUIsZBbhKov12utoYFQns0HhgB5xKLeDqtZBRqavaNjNSn7KAcObZAEcavQCRbGlVKZBArfDEHskBSR8qAoU543DVTZCOyHm5oYNDVafwHl0bAkc4jyIhh2YHEPaNpWGC0FhezsSidOgLjnfFq8CeLVxHH0nUZBMLgAZDZD"
<p></p><p>Not authenticated</p>

Not authenticated just means that this function is returning false...

What on earth am I doing wrong? As per advice given on other similar Stack Overflow answers, I've used the regex function to basically only keep alphanumeric characters but that has made no difference? It isn't a trimming issue either as that didn't work!

Any help would be much appreciated!

Thanks!

</div>
  • 写回答

1条回答 默认 最新

  • douwu0882 2015-06-29 14:05
    关注

    Here is your problem:

    if ($current == null || $current = "") {
    //                               ^ now `$current` is "", an empty string
    

    You assign a new value to $current, an empty string.

    You probably want something like:

    if ($current == null || $current == "") {
    //                               ^^ now you are comparing
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输