Published in: PHP
class Auth { var $user_id; var $username; var $password; var $ok; var $salt = "34asdf34"; var $domain = ".domain.com"; function Auth() { global $db; $this->user_id = 0; $this->username = "Guest"; $this->ok = false; if(!$this->check_session()) $this->check_cookie(); return $this->ok; } function check_session() { return $this->check($_SESSION['auth_username'], $_SESSION['auth_password']); else return false; } function check_cookie() { return $this->check($_COOKIE['auth_username'], $_COOKIE['auth_password']); else return false; } function login($username, $password) { global $db; $db->query("SELECT user_id FROM users WHERE username = '$username' AND password = '$password'"); { $this->username = $username; $this->ok = true; $_SESSION['auth_username'] = $username; return true; } return false; } function check($username, $password) { global $db; $db->query("SELECT user_id, password FROM users WHERE username = '$username'"); { { $this->username = $username; $this->ok = true; return true; } } return false; } function logout() { $this->user_id = 0; $this->username = "Guest"; $this->ok = false; $_SESSION['auth_username'] = ""; $_SESSION['auth_password'] = ""; } }
Comments
Subscribe to comments
You need to login to post a comment.

Unfortunately if you call the login or check functions without first escaping the username and password you could fall victim to SQL injection. You should make sure you escape those strings before placing them in a SQL query.
Also, whoever uses this class will need to implement the database class that you are using. Is that included somewhere on Snipplr?
check out :
http://phpmylogon.sourceforge.net/
It is way better than this code.
I don't know if it is a good idea to store(even the md5hash) the password in the cookie! It would be better if you'd store a newly generated fakesessionid in the database and the cookie.