HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Sessions and Authentication

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
andauthenticationsessions

Problem

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->pdo = $PDO;
}

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

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

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

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

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

/**
*
*/
public function get_error

Solution

The code is understandable, but it can be simplified.

Instead of:

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


You can simply:

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


Instead of:

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


You can simply:

public function get_session()
{
    return $this->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->session))
    {
        return true;
    }
    else
    {
        return false;
    }
}


You can simply:

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


Instead of:

public function get_error($key)
{
    if (isset($this->error[$key]))
    {
        return $this->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->error[$key] ?: false;
}

Code Snippets

public function unset_session()
{
    if (isset($this->session))
    {
        $this->session =  null;
    }
}
public function unset_session()
{
    $this->session =  null;
}
public function get_session()
{
    if (isset($this->session))
    {
        return $this->session;
    }
    else
    {
        return false;
    }
}
public function get_session()
{
    return $this->session;
}
public function is_logged_in()
{
    if (isset($this->session))
    {
        return true;
    }
    else
    {
        return false;
    }
}

Context

StackExchange Code Review Q#56373, answer score: 4

Revisions (0)

No revisions yet.