I've just begun creating my first user class. First, I need some clarifications.
Am I using try
and catch
correctly?
To clarify, the way I would be instantiating this object is like so:
try{
$loggedInUser = new LoggedIn/User($email,$password,$useragent);
$_SESSION['user'] = $loggedInUser;//now session holds user object
die(json_encode('type'=>'success'=>'message'=>'account_fetch_view'));
}
catch(Exception $e) {
unset($_SESSION['user']);
die(json_encode(['type'=>'error','message'=>$e]));
}
Looking at my class, will I be able to do this as I've anticipated? If not, should I just construct it as I used to do (i.e. manually from the login processing script)?
namespace LoggedIn;
class User {
public $email = NULL;
public $hash = NULL;
public $user_id = NULL;
public $user_agent = NULL;
public $sign_in = NULL;
public $csrf_token = NULL;
public $btc_addr = NULL;
public $throttle = NULL;
public $last_ajax = NULL;
public $verified = NULL;
public $reputation = NULL;
function __construct($e_mail,$p_word,$u_agent)
{
if($this->check_pass($p_word) === true && $this->check_email($e_mail) === true)
{
$result = $this->fetch_details($e_mail,$p_word);
if($result !== false && is_array($result) === true)
{
$this->email = $result['u_email'];
$this->hash = $result['u_pass'];
$this->user_id = $result['u_id'];
$this->user_agent = $u_agent;
$this->sign_in = time();
$this->csrf_token = $this->csrf_create($this->sign_in,$this->hash);
$this->btc_addr = $result['u_addr'];
$this->throttle = 0;
$this->last_ajax = ($this->sign_in - 5);
$this->verified = $result['u_verified'];
$this->reputation = $this->fetch_rep($this->user_id);
}else{
throw new Exception('Invalid Username or Password');
}
}
}
private function csrf_create($timestamp,$hash)
{
return hash('sha256', str_shuffle(openssl_random_pseudo_bytes(4) . $hash . $timestamp));
}
private function fetch_details($e_mail,$p_word)
{
}
private function check_email($e_mail)
{
}
private function check_pass($p_word)
{
}
public function log_out()
{
}
public function bid($amount,$auction)
{
}
public function buy_now($amount,$auction)
{
}
private function fetch_rep($u_id)
{
}
}