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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?