Notice: Trying to access array offset on value of type bool in C:classesuser.php on line 39

publicado por: Anonymous

Soy nuevo en PHP. Necesito implementar un System Login en un web. encontre uno en internet y lo adapte a mi proyecto. pero haciendo pruebas veo que cuando ingreso un usuario y contraseña incorrectas me da un error de:

Notice: Trying to access array offset on value of type bool in (ruta de aplicacion)/user.php on line 39

La linea 39 es la siguiente : if($this->password_verify($password,$row[‘password’]) == 1)

 <?php
    include('password.php');
    class User extends Password{

        private $_db;

        function __construct($db){
            parent::__construct();

            $this->_db = $db;
        }

        private function get_user_hash($username){

            try {
                $stmt = $this->_db->prepare('SELECT password, username, memberID FROM members WHERE username = :username AND active="Yes" ');
                $stmt->execute(array('username' => $username));

                return $stmt->fetch();

            } catch(PDOException $e) {
                echo '<p class="bg-danger">'.$e->getMessage().'</p>';
            }
        }

        public function isValidUsername($username){
            if (strlen($username) < 3) return false;
            if (strlen($username) > 17) return false;
            if (!ctype_alnum($username)) return false;
            return true;
        }

        public function login($username,$password){
            if (!$this->isValidUsername($username)) return false;
            if (strlen($password) < 3) return false;

            $row = $this->get_user_hash($username);

            if($this->password_verify($password,$row['password']) == 1){

                $_SESSION['loggedin'] = true;
                $_SESSION['username'] = $row['username'];
                $_SESSION['memberID'] = $row['memberID'];
                return true;
            }
        }

        public function logout(){
            session_destroy();
        }

        public function is_logged_in(){
            if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
                return true;
            }
        }

    }


    ?>

solución

La función get_user_hash retorna $stmt->fetch(); el cual sera false cuando el username no existe.

Esto significa que la variable $row no será un array y al intentar accederla usando $row['password'] en if($this->password_verify($password,$row['password']) == 1) provocara la notificación:

Notice: Trying to access array offset on value of type bool in (ruta de aplicacion)/user.php on line 39

Solución:

Podrías modificar el método login de la siguiente forma:

    public function login($username,$password){
        if (!$this->isValidUsername($username)) return false;
        if (strlen($password) < 3) return false;

        $row = $this->get_user_hash($username);
        // ACA - Controlamos el valor obtenido
        if (!$row) return false;

        if($this->password_verify($password,$row['password']) == 1){

            $_SESSION['loggedin'] = true;
            $_SESSION['username'] = $row['username'];
            $_SESSION['memberID'] = $row['memberID'];
            return true;
        }
    }
Respondido por: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *