首页 > 解决方案 > 在我的 php 登录系统中检查重复的用户名

问题描述

尝试为我的用户设置一个面板以查看仅限登录的内容,我目前遇到一个问题,人们可以使用相同的用户名/电子邮件注册任何人都知道如何解决这个问题我尝试了几行不同的代码建议?

这是我当前的 signup.php

<?php

require_once 'source/db_connect.php';

if(isset($_POST['signup-btn'])) {

      $username = $_POST['user-name'];
      if($username == '' || empty($username)){
          echo "<body style='background-color:#212121'><br><br><br><br><br><br><br><br><br><br><br><br><center><font size='12' color='red'>Username cannot be blank..</font></center> <br><br><br> <center><a href='register.html'><font size='5'>Click To Return.</font></a></body>";
          return false;
}
      $email = $_POST['user-email'];
      if(strpos($email, '@') == false || strpos($email, '.') == false){
        echo "<body style='background-color:#212121'><br><br><br><br><br><br><br><br><br><br><br><br><center><font size='12' color='red'>Invalid Email Adress..</font></center> <br><br><br> <center><a href='register.html'><font size='5'>Click To Return.</font></a></body>";
          return false;
      }
      $password = $_POST['user-pass'];
      if($password == '' || empty($password)){
          echo "<body style='background-color:#212121'><br><br><br><br><br><br><br><br><br><br><br><br><center><font size='12' color='red'>Password cannot be blank..</font></center> <br><br><br> <center><a href='register.html'><font size='5'>Click To Return.</font></a></body>";
          return false;
      }

      $hashed_password = password_hash($password, PASSWORD_DEFAULT);


try {
      $SQLInsert = "INSERT INTO users (username, password, email, to_date)
                   VALUES (:username, :password, :email, now())";

      $statement = $conn->prepare($SQLInsert);
      $statement->execute(array(':username' => $username, ':password' => $hashed_password, ':email' => $email));

      if($statement->rowCount() == 1) {
        header('location: success.html');
      }
    }
    catch (PDOException $e) {
      echo "Error: " . $e->getMessage();
    }

}

?>

标签: php

解决方案


推荐阅读