PHP检查密码和密码确认

时间:2019-04-12 12:49:22

标签: php forms

我正在制作一种instagram,因此我需要一个注册页面。我想检查密码是否等于或大于8个字符,以及密码和密码确认是否相同。

我尝试过制作新的安全性课或尝试-赶上。

register.php

if ( !empty($_POST)) {

    $user = new User();
        $user->setFullname($_POST['fullname']);
        $user->setUsername($_POST['username']);        
        $user->setEmail($_POST['email']);
        $user->setPassword($_POST['password']);
        $user->setPasswordConfirmation($_POST['password_confirmation']);

        if( $user->passwordsAreSecure()) {
                if($user->register()) {
                    session_start();
                    $_SESSION['email'] = $user->getEmail();
                    header('location: index.php');
                } else {
                    $error = true;  
                }
        } else {
            $error2 = true;
        }
    }

我的用户类别

public function passwordIsStrongEnough(){
                if( strlen( $this->password ) <= 8 ){
                    return false;
                }
                else {
                    return true;
                }
        }
        public function passwordsAreEqual(){
                if( $this->password == $this->passwordConfirmation ){
                    return true;
                }
                else {
                    return false;
                }
        }

        public function passwordsAreSecure(){
                if( $this->passwordIsStrongEnough() 
                    && $this->passwordsAreEqual() ){
                    return true;
                }
                else {
                    return false;
                }
        }

功能寄存器

public function register() {

                $password = Security::hash($this->password);

                try {
                    $conn = Db::getInstance();
                    $statement = $conn->prepare('INSERT INTO users (fullname, username, email, password) values (:fullname, :username, :email, :password)');
                    $statement->bindParam(':fullname', $this->fullname);
                    $statement->bindParam(':username', $this->username);
                    $statement->bindParam(':email', $this->email);
                    $statement->bindParam(':password', $password);  
                    $result = $statement->execute();
                    return($result);

                } catch ( Throwable $t ) {
                    return false;

                }

        }

我想进入if( $user->passwordsAreSecure()) {,所以有一个会话,但是现在表单字段为空,什么也没发生。

1 个答案:

答案 0 :(得分:0)

我不明白您的问题在哪里。您没有问任何问题...但是您可以像这样完成所有代码:

if (!empty($_POST)) {
    $user = new User();
    $user->setFullname($_POST['fullname']);
    $user->setUsername($_POST['username']);        
    $user->setEmail($_POST['email']);
    $user->setPassword($_POST['password']);
    $user->setPasswordConfirmation($_POST['password_confirmation']);

    if ($_POST['password'] == $_POST['password_confirmation'] 
            && strlen($_POST['password']) > 8) {
        if ($user->register()) {
            session_start();
            $_SESSION['email'] = $user->getEmail();
            header('location: index.php');
        } else {
            $error = true;  
        }
    } else {
        $error2 = true;
    }
}