/Quản lý website lưu bút trực tuyến PHP/luubut/lib/session.class.php
https://gitlab.com/phamngsinh/baitaplon_sinhvien · PHP · 80 lines · 67 code · 13 blank · 0 comment · 10 complexity · 599d993881a4758cdd55aee7b47225d0 MD5 · raw file
- <?php
- class gb_session extends gbook_sql {
- var $expire = 7200;
- var $include_path;
- var $table;
- function gb_session($path='') {
- global $GB_TBL;
- $this->table = &$GB_TBL;
- $this->gbook_sql();
- $this->connect();
- $this->include_path = $path;
- }
- function isValidSession($session,$user_id) {
- $this->query("SELECT session, last_visit from ".$this->table['auth']." WHERE session='".addslashes($session)."' and ID='".intval($user_id)."'");
- $row = $this->fetch_array($this->result);
- if ($row) {
- return ($this->expire + $row['last_visit'] > time()) ? $row["session"] : false;
- } else {
- return false;
- }
- }
- function isValidUser($user_id) {
- $this->query("SELECT username FROM ".$this->table['auth']." WHERE ID='".intval($user_id)."'");
- $this->fetch_array($this->result);
- return ($this->record) ? true : false;
- }
- function changePass($user_id, $new_password) {
- $this->query("UPDATE ".$this->table['auth']." SET password=PASSWORD('$new_password') WHERE ID='".intval($user_id)."'");
- return ($this->record) ? true : false;
- }
- function generateNewSessionID($user_id) {
- srand((double)microtime()*1000000);
- $session = md5 (uniqid (rand()));
- $timestamp = time();
- $this->query("UPDATE ".$this->table['auth']." SET session='$session', last_visit='$timestamp' WHERE ID='".intval($user_id)."'");
- return $session;
- }
- function checkPass($username,$password) {
- $this->query("SELECT ID FROM ".$this->table['auth']." WHERE username='".addslashes($username)."' and password=PASSWORD('".addslashes($password)."')");
- $this->fetch_array($this->result);
- return ($this->record) ? $this->record["ID"] : false;
- }
- function checkSessionID() {
- global $username, $password, $session, $uid;
- if (isset($session) && isset($uid)) {
- if (get_magic_quotes_gpc()) {
- $session = stripslashes($session);
- }
- return ($this->isValidSession($session,$uid)) ? array("session" => "$session", "uid" => "$uid") : false;
- } elseif (isset($username) && isset($password)) {
- if (get_magic_quotes_gpc()) {
- $username = stripslashes($username);
- $password = stripslashes($password);
- }
- $ID = $this->checkPass($username,$password);
- if ($ID) {
- $session = $this->generateNewSessionID($ID);
- return array("session" => "$session", "uid" => "$ID");
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- }
- ?>