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
        }
    
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

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