PageRenderTime 300ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1. <?php
  2. class gb_session extends gbook_sql {
  3. var $expire = 7200;
  4. var $include_path;
  5. var $table;
  6. function gb_session($path='') {
  7. global $GB_TBL;
  8. $this->table = &$GB_TBL;
  9. $this->gbook_sql();
  10. $this->connect();
  11. $this->include_path = $path;
  12. }
  13. function isValidSession($session,$user_id) {
  14. $this->query("SELECT session, last_visit from ".$this->table['auth']." WHERE session='".addslashes($session)."' and ID='".intval($user_id)."'");
  15. $row = $this->fetch_array($this->result);
  16. if ($row) {
  17. return ($this->expire + $row['last_visit'] > time()) ? $row["session"] : false;
  18. } else {
  19. return false;
  20. }
  21. }
  22. function isValidUser($user_id) {
  23. $this->query("SELECT username FROM ".$this->table['auth']." WHERE ID='".intval($user_id)."'");
  24. $this->fetch_array($this->result);
  25. return ($this->record) ? true : false;
  26. }
  27. function changePass($user_id, $new_password) {
  28. $this->query("UPDATE ".$this->table['auth']." SET password=PASSWORD('$new_password') WHERE ID='".intval($user_id)."'");
  29. return ($this->record) ? true : false;
  30. }
  31. function generateNewSessionID($user_id) {
  32. srand((double)microtime()*1000000);
  33. $session = md5 (uniqid (rand()));
  34. $timestamp = time();
  35. $this->query("UPDATE ".$this->table['auth']." SET session='$session', last_visit='$timestamp' WHERE ID='".intval($user_id)."'");
  36. return $session;
  37. }
  38. function checkPass($username,$password) {
  39. $this->query("SELECT ID FROM ".$this->table['auth']." WHERE username='".addslashes($username)."' and password=PASSWORD('".addslashes($password)."')");
  40. $this->fetch_array($this->result);
  41. return ($this->record) ? $this->record["ID"] : false;
  42. }
  43. function checkSessionID() {
  44. global $username, $password, $session, $uid;
  45. if (isset($session) && isset($uid)) {
  46. if (get_magic_quotes_gpc()) {
  47. $session = stripslashes($session);
  48. }
  49. return ($this->isValidSession($session,$uid)) ? array("session" => "$session", "uid" => "$uid") : false;
  50. } elseif (isset($username) && isset($password)) {
  51. if (get_magic_quotes_gpc()) {
  52. $username = stripslashes($username);
  53. $password = stripslashes($password);
  54. }
  55. $ID = $this->checkPass($username,$password);
  56. if ($ID) {
  57. $session = $this->generateNewSessionID($ID);
  58. return array("session" => "$session", "uid" => "$ID");
  59. } else {
  60. return false;
  61. }
  62. } else {
  63. return false;
  64. }
  65. }
  66. }
  67. ?>