Location: PHPKode > scripts > PHP Desk Library > php-desk-library/cus.php
<?
  define("CUS_NORMAL", 1);
  define("CUS_SUSPENDED", 2);
  define("CUS_WAITING", 3);
  
  class cus {
    var $logged_in;
    var $current;
    function cus() {
      $this->logged_in = 0;
      $cus = new dbquery("SELECT * FROM users WHERE id='{$_COOKIE["id"]}'", 1);
      if (($_COOKIE["id"]) && ($cus->row->password == $_COOKIE["auth"])) {
          if ($cus->row->status == CUS_SUSPENDED) { $this->logged_in = 0; return CUS_SUSPENDED; }
          else if ($db->row->status == CUS_WAITING) { $this->logged_in = 0; return CUS_WAITING; }
          $this->logged_in = 1;
          $this->current = $db->row;
          return CUS_NORMAL;
      } else {
        $this->logout();
        return 0;
      }
    } 

    function login($username, $password) {
      $this->logout();
      $login = new dbquery("SELECT * FROM users WHERE username='$username' AND password='" . md5($password) . "'", 1);
      
      if ($login->numrows > 0) {
        if ($login->row->status == CUS_SUSPENDED) { $this->logged_in = 0; return CUS_SUSPENDED; }
        else if ($login->row->status == CUS_WAITING) { $this->logged_in = 0; return CUS_WAITING; }
        $this->logged_in = 1;
        setcookie("id", $login->row->id);
        setcookie("auth",$login->row->password);
        $this->current = $login->row;
        return 1;
      } else {
        $this->logged_in = 0;
        unset($this->current);
        return 0;
      }
    }

    function logout() {
      setcookie("id", "");
      setcookie("auth", "");
      $this->logged_in = 0;
      unset($this->current);
      return 1;
    }

    function user($username, $property) {
      $user = new dbquery("SELECT $property FROM users WHERE username='$username'", 1);
      if (isset($user->row->$property)) { return $user->row->$property; } else { return 0; }
    }

    function user2id($username) {
      $id = new dbquery("SELECT id FROM users WHERE username='$username'", 1);
      if (isset($id->row->id)) { return $id->row->id; }
    }

    function id2user($id) {
      $username = new dbquery("SELECT username FROM users WHERE id='$id'", 1);
      return $username->row->username;
    }
  }
?>
Return current item: PHP Desk Library