Confusing for dual use php session and $this variable

  php, session

The php class named user contain five properties and two function user_login () and user_info().
the code is working perfect but i am not sure if it is best practice, i use the class member for internal use within class but session can do the job too so..

My question is: can i depend on session or properties or both ?

class user
{ 
 public $user_id;
 public $user_role_id;
 public $user_email;
 public $user_first_name;
 public $user_last_name;        


public function __construct() 
{
if (!empty($_SESSION['user_id']))
{
$this->user_id=$_SESSION['user_id'];
}
else
{$_SESSION['user_id']=null;
$this->user_id=$_SESSION['user_id'];
}

}
 public function user_login()
{
print"  <div style='display:block; margin:auto; width:100%; margin-top:5px; margin-bottom:5px;text-align: center'>
            <FORM ACTION='index.php?division=login' METHOD='POST'>";

    print" <fieldset style='display:block;'>
            <legend >$translate[login]:</legend>            
            <div style='display:table; '>
              <div style='display: table-row; '>
               <div style='display: table-cell;'>$email</div>
               <div style='display: table-cell;'><input name='get_email' style='min-width: 350px; width:100%' type='email' placeholder='email' required></div>
              </div>
               <div style='display: table-row;'>
                <div style='display: table-cell; '>$password</div>
                 <div style='display: table-cell;'><input name='get_password' style='max-width: 350px; width:100%' type='password' placeholder='password' required></div>
              </div>
            </div>
            </fieldset>
            </div>";    
        
    
    print"<div style='display:block; text-align: center'><input style='font-size: 14pt; padding: 5px;' name='login_submit' type='submit' value='{$submit'}'/></div>          
</div>
        </FORM>";   
}

 public function user_info()
{
global $database;

if (!empty($_SESSION['user_id']))
{
    $select_user="SELECT user.user_id, 
                         user.user_email,
                          user.user_first_name,
                          user.user_last_name,
                          role.role_id,
                          role.en_role,
                          role.ar_role,
                          user_role.user_id,user_role.role_id
                                                        
                  FROM user,role,user_role 
                            
                  WHERE user.user_id='$_SESSION[user_id]'
                  AND user.user_id=user_role.user_id 
                  AND role.role_id=user_role.role_id";

    $query_user = mysqli_query($database->link, $select_user) or die("Error: ".mysqli_error($database->link));

    while($fetch_row=mysqli_fetch_array($query_user,MYSQLI_ASSOC))
    {
          $this->user_role_id=$fetch_row['role_id'];
          $this->user_email=$fetch_row['user_email'];
          $this->user_first_name=$fetch_row['user_first_name'];
          $this->user_last_name=$fetch_row['user_last_name'];         
       
       
        $_SESSION['user_role_id']=$fetch_row['role_id'];
        $_SESSION['user_email']=$fetch_row['user_email'];
        $_SESSION['user_first_name']=$fetch_row['user_first_name'];
        $_SESSION['user_last_name']=$fetch_row['user_last_name'];   
        
      
       
       
    }
}


else
{

print"Not login!!"; 

}
}



Source: Ask PHP

LEAVE A COMMENT