Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Login Class
- class login extends account {
- private $username;
- private $password;
- private $remember;
- private $sess_id;
- //Account Construction Function
- public function __construct($username,$password,$remember) {
- $this->username = parent::clear_string($username);
- $this->password = parent::clear_string($password);
- $this->remember = $remember;
- $error_check = $this->error_check();
- if(!$error_check) {
- $this->sess_id = $this->set_sessID();
- $session = $this->create_session();
- $cookie = $this->set_cookie();
- if(!$cookie || !$session) {
- return false;
- } else {
- return true;
- }
- }
- }
- //Login form error check
- private function error_check() {
- //Status checker (of whether a user is logged in) will be elaborated on
- //This is a temporary spot - it will be changed in the future
- if(parent::check_status()) {
- throw new Exception('You are already logged in.');
- return true;
- }
- if(empty($this->username) || empty($this->password)) {
- throw new Exception('One or more fields were left blank.');
- return true;
- }
- if(!ctype_alnum($this->username) || !ctype_alnum($this->password)) {
- throw new Exception('Only alphanumeric characters may be used.');
- return true;
- }
- if(!parent::find_user($this->username)) {
- throw new Exception('User \''.$this->username.'\' does not exist in the database.');
- return true;
- }
- if(!$this->check_pswd()) {
- throw new Exception('Password entered for user \''.$this->username.'\' was incorrect.');
- return true;
- }
- if(!parent::check_validity($this->username)) {
- throw new Exception('User \''.$this->username.'\' has not been validated. Please check your email for a validation link, or contact an administrator if you did not recieve an email.');
- return true;
- }
- return false;
- }
- //Check if the password matches the stored password
- private function check_pswd() {
- $sql_pswd = parent::get_info($this->username,'password');
- if(md5($this->password) != $sql_pswd) {
- return false;
- } else {
- return true;
- }
- }
- //Create the session ID
- private function set_sessID() {
- $session = parent::str_rand(20);
- $sess_id = md5($this->username.$this->password.$session);
- return $sess_id;
- }
- //Set the login cookie
- private function set_cookie() {
- switch ($this->remember) {
- case true:
- $cookie_expire = time()+60+60+24+30;
- break;
- case false:
- $cookie_expire = 0;
- break;
- }
- $cookie = setcookie('knoxius_account',$this->sess_id,$cookie_expire,'/');
- if(!$cookie) {
- throw new Exception('You could not be logged in because you do not have cookies turned on.');
- return false;
- } else {
- return true;
- }
- }
- //Set the login session
- private function create_session() {
- $_SESSION['knoxius_account'] = $this->sess_id;
- $query = 'INSERT INTO session VALUES(NULL,\''.$this->username.'\',\''.$this->sess_id.'\')';
- $create_sess = mysql_query($query);
- if(!$create_sess) {
- throw new Exception('An unknown error occurred and you were not logged in.');
- return false;
- } else {
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement