00001 <?php
00049 class Session {
00050 private $enabled;
00051 private $id_area;
00052 private $svprefix;
00063 function __construct() {
00064 global $cfg;
00065 $this->id_area = $cfg->getAreaId();
00066 $this->svprefix = $cfg->getConfig("SVPREFIX");
00067
00068 session_start();
00069
00070 $this->enabled = (isset($_COOKIE[session_name()]));
00071 if (!$this->enabled) { return; }
00072
00073
00074 mysql_query("DELETE FROM sessions WHERE (id_area = " . $this->id_area . ") " .
00075 "AND (date_lastused < " . (time() - $cfg->getConfig("SESSION_LIFETIME") * 60) . ")");
00076
00077
00078
00079 $res = mysql_query("SELECT remote_ip FROM sessions " .
00080 "WHERE (" . $this->id_area . ") ".
00081 "AND (id = '" . session_id() . "')");
00082 if (mysql_num_rows($res) > 0) {
00083 $line = mysql_fetch_assoc($res);
00084 if ($line['remote_ip'] == $_SERVER['REMOTE_ADDR']) {
00085 mysql_query("UPDATE sessions SET date_lastused = " . time() .
00086 " WHERE (id_area = " . $this->id_area . ") " .
00087 "AND (id = '" . session_id() . "')");
00088 } else {
00089 if (session_regenerate_id()) {
00090 $this->registerSession();
00091 } else {
00092 return;
00093 }
00094 }
00095 } else {
00096 session_unset();
00097 $_SESSION = array();
00098 $this->registerSession();
00099 }
00100 mysql_free_result($res);
00101 }
00102
00106 private function registerSession() {
00107 mysql_query("INSERT INTO sessions (id, id_area, remote_ip, date_lastused, id_user) " .
00108 "VALUES ('" . session_id() . "', " . $this->id_area . ", '" . $_SERVER['REMOTE_ADDR'] . "', " . time() . ", 0)");
00109 }
00110
00116 public function setUseridInSession($uid) {
00117 mysql_query("UPDATE sessions SET id_user = " . $uid .
00118 " WHERE (id_area = " . $this->id_area . ") " .
00119 "AND (id = '" . session_id() . "')");
00120 }
00121
00128 public function setValue($varname, $value) {
00129 if ($this->enabled) {
00130 $_SESSION[$this->svprefix . $varname] = $value;
00131 }
00132 }
00133
00140 function getValue($varname) {
00141 if ($this->enabled && isset($_SESSION[$this->svprefix . $varname])) {
00142 return $_SESSION[$this->svprefix . $varname];
00143 }
00144 }
00145
00151 function destroy() {
00152 if ($this->enabled) {
00153
00154 mysql_query("DELETE FROM sessions WHERE id = '" . session_id() . "'");
00155
00156 $_SESSION = array();
00157
00158 if (isset($_COOKIE[session_name()])) {
00159 setcookie(session_name(), "", time() - 86400, "/");
00160 }
00161
00162 session_destroy();
00163 }
00164 }
00165 }
00166 ?>