dongshanfan1941 2018-10-17 14:20
浏览 196

PHP - 检查用户名是否存在或提交的用户名是否为当前

I'm trying to check if the entered username already exists or if the entered username is the current username.

I've Googled various SO questions but none seem to check if the current username is the submitted one.

The problem with the following code; it doesn't matter if the username is taken or not, it will still let you save.

$stmt = $engine->runQuery("SELECT user_name, user_email FROM users WHERE user_name=:username OR user_email=:email");
$stmt->execute(array(':username'=>$username, ':email'=>$email));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

if(strtolower($row['user_name']) == strtolower($username) || $username !== $row['user_name']) {
    $engine->authapi(false, 'Sorry, username is already taken. Please choose a different one.');
} elseif(strtolower($row['user_email']) == strtolower($email) && $email !== $_SESSION['user_email']) {
    $engine->authapi(false, 'Email is already registered. You cannot use the same emails for multiple accounts.');
} else {
    // save
}

How can I make it so it checks if the username is taken or not, and at the same time check if the submitted username is the current username (if so, let the user save)?

  • 写回答

1条回答 默认 最新

  • doumo3903 2018-10-17 15:06
    关注

    Actually, there are several issues in your code.

    1) Your SQL. You can fetch more than one row here, for example you have two entries in your database, username: maio290, e-mail: a@foo.bar and username: maio291, e-mail: b@foo.bar. Now your user enteres username: maio290 and e-mail: b@foo.bar which will result in two entries selected. Most likely an edge case, but a valid one.

    2) Your if: You're comparing strtolower($row['user_name']) == strtolower($username) OR $username !== $row['user_name']) - the second one doesn't make any sense with your error. Since that means: "hey, your user is not in our database, please take a different one" Also, the first comparision could be a lot nicer with using strcasecmp.

    I would really split these two options, since it's a lot better to read and you don't have the problem with two selectable rows. Also, you let your database handle the comparision.

    Therefore I would write the code like that:

    <?PHP
    // Select if username is taken
    $stmt = $engine->runQuery("SELECT user_name FROM users WHERE user_name=:username");
    $stmt->execute(array(':username'=>$username));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    
    
    if(count($row) != 0)
    {
        $engine->authapi(false, 'Sorry, username is already taken. Please choose a different one.');
        // I would actually return here, so we wouldn't need an else
    }
    else
    {
        // check if e-mail is registred
        $stmt = $engine->runQuery("SELECT user_email FROM users WHERE user_email=:email");
        $stmt->execute(array(':email'=>$email));
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
    
        if(count($row) != 0)
        {
            $engine->authapi(false, 'Email is already registered. You cannot use the same emails for multiple accounts.');
        }
        else
        {
            // store
        }
    
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?