<?php class Session { var $session_id; var $table = SESSIONS_TABLE; # Konstruktor, laczy z baza danych function Session($config) { $this->db = new sql_layer; $this->db->sql_open($config); return true; } # start sesji function start() { $this->garbage(); if($_GET['sid'] != '') { $sessionid = $_GET['sid']; } else { $sessionid = $this->session_id; } if($sessionid) { $sid = $sessionid; $this->db->sql_query(\"UPDATE \".$this->table.\" SET session_time=UNIX_TIMESTAMP() WHERE session_id='\".$sid.\"'\"); $this->session_id = ($sid != '') ? $sid : $this->session_id; } else { $this->db->sql_query(\"INSERT INTO \".$this->table.\" VALUES('\".$sid.\"', UNIX_TIMESTAMP(), '')\") or die($this->db->sql_error()); $this->session_id = $sid; } return true; } # Dodajemy dane do sesji (jezeli $vars jest puste to tylko aktualizujemy \"session_time\") function write($vars = '') { if($vars == '') { $this->db->sql_query(\"UPDATE \".$this->table.\" SET session_time=UNIX_TIMESTAMP() WHERE session_id='\".$this->session_id.\"'\") or die($this->db->sql_error()); } else { $data = $this->read(); { } else { $info = $vars; } $this->db->sql_query(\"UPDATE \".$this->table.\" SET session_time=UNIX_TIMESTAMP() WHERE session_id='\".$this->session_id.\"'\") or die($this->db->sql_error()); $this->db->sql_query(\"UPDATE \".$this->table.\" SET session_value='\".serialize($info).\"' WHERE session_id='\".$this->session_id.\"'\") or die($this->db->sql_error()); } return true; } # Zamkniecie sesje uzytkownika (usuwamy sesje uzytkownika i wrzucamy pusta sesje o tym samym id) function close() { $this->db->sql_query(\"DELETE FROM \".$this->table.\" WHERE session_id='\".$this->session_id.\"'\") or die($this->db->sql_error()); $this->db->sql_query(\"INSERT INTO \".$this->table.\" VALUES('\".$this->session_id.\"', UNIX_TIMESTAMP(), '')\") or die($this->db->sql_error()); return true; } # Odczytujemy wartosci sesji function read() { $this->db->sql_query(\"SELECT session_value FROM \".$this->table.\" WHERE session_id='\".$this->session_id.\"' LIMIT 1\") or die($this->db->sql_error()); $row = $this->db->sql_fetch(); } # Wiadomo... wywolywane przy start() function garbage() { $this->db->sql_query(\"DELETE FROM \".$this->table.\" WHERE session_time<'\".$time.\"'\") or die($this->db->sql_error()); } } function append_sid($url) { { $url.= '&sid='.$session->session_id; } else { $url.= '?sid='.$session->session_id; } return $url; } # Startujemy sesje $session = new Session($config['sql']); $session->start(); ?>
Na każdej stronie (prócz login.php) dodaję taki kod:
<?php if($_GET['sid'] == \"\") { $adres = $_SERVER['PHP_SELF']; $i=0; foreach($_GET as $key => $value) { $adres.= ($i==0) ? '?' : '&'; $adres.= $key.'='.$value; $i++; } } $sessiondata = $session->read(); $session->write(); ?>
Czyli odczytuję dane sesji do zmiennej $sessiondata i aktualizuje obecny czas w sesji (session_time). Funkcja append_sid dodaje do adresu identyfikator sesji.
Loguję się za pomocą tego kodu:
<?php # ... tutaj konfiguracja itp itd. include('config.php'); include('includes/sql_layer.php'); include('includes/constant.php'); include('includes/session.php'); if($_GET['m'] == 'logout') { $session->close(); } else { if($sessiondata['logged_in'] == false) { if($_POST['username']!='' && $_POST['password']!='') { # loggin in user $db->sql_query(\"SELECT * FROM `\".USERS_TABLE.\"` WHERE username = '\".$_POST['username'].\"' AND user_password = '\".md5($_POST['password']).\"'\") or die($db->sql_error()); if($row = $db->sql_fetch()) { $ses['logged_in'] = 1; $ses['user_id'] = $row['user_id']; $session->write($ses); } else { } } else { } } else { } } ?>
Logowanie działa na takiej zasadzie: Zamiast usuwać sesję i tworzyć nową użytkownika zalogowanego, to poprostu uaktualniamy wartość sesji poprzez wywołanie $session->write($ses) na tym identyfikatorze.
Logowanie działa wporządku, nawigacja po stronie tak samo. Jedyna rzecz, która nie działa to wylogowywanie

Proszę o pomoc!!