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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分