Sessions and Authentication I've a feeling I'm overdoing my Auth class, and that it could be done in a simpler and more understandable way.

Could you give me advice on this, please?

This is for my learning process and I just want to learn. This is why I am 'reinventing the wheel'.

class Auth
{

    protected $pdo;
    protected $session = null;
    protected $error = array();

    /**
    * 
    */
    function __construct($PDO)
    {
        $this-&gt;pdo = $PDO;
    }

    /**
    * 
    */
    public function authenticate_user($password,$email)
    {
        try
        {
            $select_user = $this-&gt;pdo-&gt;prepare('SELECT * FROM users WHERE user_email = :email LIMIT 1');
            $select_user-&gt;execute(array(':email' =&gt; $email));
            $user_data = $select_user-&gt;fetch(PDO::FETCH_ASSOC);
            if (password_verify($password,$user_data['user_pass']))
            {
                $this-&gt;set_session($user_data);
                return true;
            }
            else
            {
                $this-&gt;error['authenticate_user'] = 'Incorrect email or password';
            }
        }
        catch (PDOException $e)
        {
            echo $e-&gt;getMessage();
        }
    }

    /**
    * 
    */
    public function set_session($user_data)
    {
        if ($user_data)
        {
            $this-&gt;session = $user_data;
        }
    }

    /**
    * 
    */
    public function unset_session()
    {
        if (isset($this-&gt;session))
        {
            $this-&gt;session =  null;
        }
    }

    /**
    * 
    */
    public function get_session()
    {
        if (isset($this-&gt;session))
        {
            return $this-&gt;session;
        }
        else
        {
            return false;
        }
    }

    /**
    * 
    */
    public function is_logged_in()
    {
        if (isset($this-&gt;session))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
    * 
    */
    public function get_error($key)
    {
        if (isset($this-&gt;error[$key]))
        {
            return $this-&gt;error[$key];
        }
        else
        {
            return false;
        }
    }

}


And on the login.php:

  if (!empty($_POST))
  {
      $response = $Auth-&gt;authenticate_user($_POST['password'],$_POST['email']);
  }

  session_start();

  if (isset($response))
  {
      $_SESSION = $Auth-&gt;get_session();
  }

  print_r($_SESSION);

  echo $Auth-&gt;get_error('authenticate_user');


And in the file that loads all the other files (at the top of everything):

if (isset($_SESSION)
{
  $Auth-&gt;set_session($_SESSION);
}
The code is understandable, but it can be simplified.

Instead of:


public function unset_session()
{
    if (isset($this-&gt;session))
    {
        $this-&gt;session =  null;
    }
}



You can simply:

public function unset_session()
{
    $this-&gt;session =  null;
}




Instead of:


public function get_session()
{
    if (isset($this-&gt;session))
    {
        return $this-&gt;session;
    }
    else
    {
        return false;
    }
}



You can simply:

public function get_session()
{
    return $this-&gt;session;
}


(if you don't mind that it returns a falsy value (null) instead of a real false)



Instead of:


public function is_logged_in()
{
    if (isset($this-&gt;session))
    {
        return true;
    }
    else
    {
        return false;
    }
}



You can simply:

public function is_logged_in()
{
    return isset($this-&gt;session);
}




Instead of:


public function get_error($key)
{
    if (isset($this-&gt;error[$key]))
    {
        return $this-&gt;error[$key];
    }
    else
    {
        return false;
    }
}



As of PHP 5.3, using the short ternary operator, you can simply:

public function get_error($key)
{
    return $this-&gt;error[$key] ?: false;
}