irwan

PHP BASIC (Login class)

Feb 15th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1. <?php  
  2.  class Auth
  3.     {
  4.     var $user_id;
  5.     var $username;
  6.     var $password;
  7.     var $ok;
  8.     var $salt = "34asdf34";
  9.     var $domain = ".domain.com";
  10.      
  11.     function Auth()
  12.     {
  13.     global $db;
  14.      
  15.     $this->user_id = 0;
  16.     $this->username = "Guest";
  17.     $this->ok = false;
  18.      
  19.     if(!$this->check_session()) $this->check_cookie();
  20.      
  21.     return $this->ok;
  22.     }
  23.      
  24.     function check_session()
  25.     {
  26.     if(!empty($_SESSION['auth_username']) && !empty($_SESSION['auth_password']))
  27.     return $this->check($_SESSION['auth_username'], $_SESSION['auth_password']);
  28.     else
  29.     return false;
  30.     }
  31.      
  32.     function check_cookie()
  33.     {
  34.     if(!empty($_COOKIE['auth_username']) && !empty($_COOKIE['auth_password']))
  35.     return $this->check($_COOKIE['auth_username'], $_COOKIE['auth_password']);
  36.     else
  37.     return false;
  38.     }
  39.      
  40.     function login($username, $password)
  41.     {
  42.     global $db;
  43.     $db->query("SELECT user_id FROM users WHERE username = '$username' AND password = '$password'");
  44.     if(mysql_num_rows($db->result) == 1)
  45.     {
  46.     $this->user_id = mysql_result($db->result, 0, 0);
  47.     $this->username = $username;
  48.     $this->ok = true;
  49.      
  50.     $_SESSION['auth_username'] = $username;
  51.     $_SESSION['auth_password'] = md5($password . $this->salt);
  52.     setcookie("auth_username", $username, time()+60*60*24*30, "/", $this->domain);
  53.     setcookie("auth_password", md5($password . $this->salt), time()+60*60*24*30, "/", $this->domain);
  54.      
  55.     return true;
  56.     }
  57.     return false;
  58.     }
  59.      
  60.     function check($username, $password)
  61.     {
  62.     global $db;
  63.     $db->query("SELECT user_id, password FROM users WHERE username = '$username'");
  64.     if(mysql_num_rows($db->result) == 1)
  65.     {
  66.     $db_password = mysql_result($db->result, 0, 1);
  67.     if(md5($db_password . $this->salt) == $password)
  68.     {
  69.     $this->user_id = mysql_result($db->result, 0, 0);
  70.     $this->username = $username;
  71.     $this->ok = true;
  72.     return true;
  73.     }
  74.     }
  75.     return false;
  76.     }
  77.      
  78.     function logout()
  79.     {
  80.     $this->user_id = 0;
  81.     $this->username = "Guest";
  82.     $this->ok = false;
  83.      
  84.     $_SESSION['auth_username'] = "";
  85.     $_SESSION['auth_password'] = "";
  86.      
  87.     setcookie("auth_username", "", time() - 3600, "/", $this->domain);
  88.     setcookie("auth_password", "", time() - 3600, "/", $this->domain);
  89.     }
  90.      
  91.     }
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment