使用php和MYSQL验证用户名和密码

时间:2016-02-27 10:14:51

标签: php pdo

我正在使用基本的PHP创建帐户并登录我在网上找到的模板。但是,在创建帐户后,每次我尝试登录时都会收到错误的用户名/密码。 我是PHP新手,一直在试图找出错误的位置。所以我想知道这里有人能帮忙吗?

 public function __construct( $data = array() ) {
         if( isset( $data['username'] ) ) $this->username = stripslashes( strip_tags( $data['username'] ) );
         if( isset( $data['password'] ) ) $this->password = stripslashes( strip_tags( $data['password'] ) );
     }

     public function storeFormValues( $params ) {
        //store the parameters
        $this->__construct( $params ); 
     }

     public function userLogin() {
         $success = false;
         try{
            $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); 
            $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            $sql = "SELECT * FROM user_accounts WHERE username = :username AND password = :password LIMIT 1";
            $stmt = $con->prepare( $sql );
            $stmt->bindValue( ":username", $this->username, PDO::PARAM_STR );
            $stmt->bindValue( ":password", hash("whirlpool", $this->password . $this->salt), PDO::PARAM_STR );
            $stmt->execute();

            $valid = $stmt->fetchColumn();

            if( $valid ) {
                $success = true;
            }

            $con = null;
            return $success;
         }catch (PDOException $e) {
             echo $e->getMessage();
             return $success;
         }
     }

     public function register() {
        $correct = false;
            try {
                $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
                $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                $sql = "INSERT INTO user_accounts(username, password) VALUES(:username, :password)";

                $stmt = $con->prepare( $sql );
                $stmt->bindValue( ":username", $this->username, PDO::PARAM_STR );
                $stmt->bindValue( ":password", hash("whirlpool", $this->password . $this->salt), PDO::PARAM_STR );
                $stmt->execute();
                return "Registration Successful <br/> <a href='index.php'>Login Now</a>";
            }catch( PDOException $e ) {
                return $e->getMessage();
            }
     }

 }

?>

0 个答案:

没有答案