Home > Back-end >  trying to get property 'username' of non-object in home.php file
trying to get property 'username' of non-object in home.php file

Time:07-25

In my PHP project i want to get the logged in users data ,and After i logged in i am redirect to home.PHP page and i cant access $user data here is my code, any one who has experience before in this error please help me in my user.PHP file

<?php

class User{
    public $db,$id;

    public function __construct(){
        $db = new DB;

        $this->db =  $db->connect();
        $this->id = $this->ID();
    }
    public function ID(){
        if($this->isLoggedIn()){
            return $_SESSION['id'];
        }
    }
    public function emailExist($email){
        $stmt = $this->db->prepare("SELECT *FROM `users` WHERE `email`=:email ");
        $stmt->bindParam(":email",$email,PDO::PARAM_STR);
        $stmt->execute();
        $user = $stmt->fetch(PDO::FETCH_OBJ);
        if (!empty($user)) {
            return $user;
        }else{
            return false;
        }


    }
    public function userData($id=''){
        $id = ((!empty($id)) ? '$id' : '$this->id');
        $stmt = $this->db->prepare("SELECT *FROM `users` WHERE `id`=:id ");
        $stmt->bindParam(":id",$id,PDO::PARAM_STR);
        $stmt->execute();
         return $stmt->fetch(PDO::FETCH_OBJ);
        
    }

    public function isLoggedIn(){
        return ((isset($_SESSION['id'])) ? true : false);
    }

}

and in my home.php

include_once 'session.php';
include_once 'connection.php';
include_once 'utilities.php';
include_once 'user.php';

if(!$userObj->isLoggedIn()){
    $userObj->redirect('index.php');
}
$user = $userObj->userData();

?>
<div>
                        <span ><?php echo $user->username;?></span>
                    </div>

in my session.php

<?php
session_start();
require 'connection.php';
require 'user.php';

$userObj = new User;

define('BASE_URL','http://localhost:8081/insa/');

login.php

<?php
include_once 'session.php';
include_once 'connection.php';
include_once 'utilities.php';

if(isset($_POST['loginBtn'])){
    //array to hold errors
    $form_errors = array();

//validate
    $required_fields = array('username', 'password');
    $form_errors = array_merge($form_errors, check_empty_fields($required_fields));

    if(empty($form_errors)){

        //collect form data
        $user = $_POST['username'];
        $password = $_POST['password'];

        //check if user exist in the database
        $sqlQuery = "SELECT * FROM users WHERE username = :username";
        $statement = $db->prepare($sqlQuery);
        $statement->execute(array(':username' => $user));

       while($row = $statement->fetch()){
           $id = $row['id'];
           $hashed_password = $row['password'];
           $username = $row['username'];

           if(password_verify($password, $hashed_password)){
               $_SESSION['id'] = $id;
               $_SESSION['username'] = $username;
               header("location: home.php");
           }else{
               $result = "<p style='padding: 20px; color: red; border: 1px solid gray;'> Invalid username or password</p>";
           }
       }

    }else{
        if(count($form_errors) == 1){
            $result = "<p style='color: red;'>There was one error in the form </p>";
        }else{
            $result = "<p style='color: red;'>There were " .count($form_errors). " error in the form </p>";
        }
    }
}
?>

here is my login.php file

CodePudding user response:

My guess is that your $userObj contains FALSE, because the requested data could not be found. Try to var_dump($userObj); before you use it, to see what value it holds.

  •  Tags:  
  • php
  • Related