Class for working with users and passwords in a PHP CMS
New code (optimizations/simplifications, see comments in code for explanations):

class Users {
    private static $key, $table, $logtable;

    public static function init($key) {
        self::$key = $key;
        self::$table = DBPREFIX.$key.'s';
        self::$logtable = self::$table.'_access_log';
    }

    public static function isLogged() {
        if(isset($_SESSION[self::$key]) and isset($_SESSION[self::$key]['uid'])) { # Combined ifs
            $uid = intval($_SESSION[self::$key]['uid']);
            $user = DB::select()-&gt;from(self::$table)-&gt;where('id', '=', $uid)-&gt;execute()-&gt;object();
            return count($user) &gt; 0; # Simplified
        }
        return false; # Removed unnecesary elses
    }

    public static function userExist($login) {
        $user = DB::select()-&gt;from(self::$table)-&gt;where('login', '=', $login)-&gt;execute()-&gt;object();
        return count($user) &gt; 0; # Simplified
    }

    public static function getUser($login) {
        $user = DB::select()-&gt;from(self::$table)-&gt;where('login', '=', $login)-&gt;execute()-&gt;object();
        if(count($user) &gt; 0) {
            return $user[0];
        }
        return false; # Removed unnecessary else
    }

    public static function generateHash($password) {
        $salt = self::generateSalt();
        $hashedPassword = crypt($password, $salt);
        return array('hashed_password' =&gt; $hashedPassword, 'salt' =&gt; $salt);
    }

    private static function generateSalt($blowfishPre = '$2y$10$', $blowfishEnd = '$', $allowedChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./', $saltLength = 20) { # Added arguments for future customization
        $charsLength = strlen($allowedChars);
        $salt = "";
        for($i=0; $i &lt; $saltLength; $i++)
        {
            $salt .= $allowedChars[mt_rand(0,$charsLength)];
        }
        return $blowfishPre . $salt . $blowfishEnd; # Removed unnecessary variable
    }

    public static function comparePasswords($input, $uid) {
        $user = DB::select()-&gt;from(self::$table)-&gt;where('id', '=', $uid)-&gt;execute()-&gt;object();
        if(count($user) &gt; 0) {
            $user = $user[0];
            $password = $user-&gt;password;
            return crypt($input, $password) === $password;
        }
        return false; # Removed unnecessary else
    }

    public static function userExit() {
        if(isset($_SESSION[self::$key])) {
            unset($_SESSION[self::$key]);
        }
    }

    public static function changePassword($password, $uid) {
        $newPassword = self::generateHash($password);
        if(isset($newPassword['hashed_password']) &amp;&amp; isset($newPassword['salt'])) {
            DB::update(self::$table)-&gt;set(array('password' =&gt; $newPassword['hashed_password'], 'salt' =&gt; $newPassword['salt']))-&gt;where('id', '=', $uid)-&gt;execute();
        }
    }

    public static function updateTime($uid) {
        DB::update(self::$table)-&gt;set(array('last_login' =&gt; time()))-&gt;where('id', '=', $uid)-&gt;execute();
    }

    public static function writeAccessLog($uid, $success) {
        DB::insert(self::$logtable)-&gt;set(array('admin_id' =&gt; $uid, 'date' =&gt; time(), 'success' =&gt; $success ? '1' : '0'))-&gt;execute(); # Inlined success var.
    }
}