Basic Sessions Go to this page"; ?> The above example shows you that I create a session variable named "real_name" storing my name as the value. Then on the second page, I print out the session variable and my name will show up. LetÂ’s do another example: When you first access that page, it will display 1. Try to refresh the page and the number will grow. To destroy or delete an existing session variable, you can use the unset command. unset($_SESSION["session_name"]); Or if you want to delete all session variables (and the session itself), you can do it by using the destroy command. session_destroy(); The destroy command is usually used to log-off a user from the membership area. LetÂ’s make a membership area for our example.
Running Membership with Sessions In the first page, I stated that sessions are usually used to create a membership management. Now, I want to show you a little snippet to create a login system, which you have to complete to create your own membership management. Your username is empty."; if (trim($password) == "") $error .= "
Another Example Still don’t understand how to use sessions? Here is another example for you. Let’s make an online user counter using sessions. Step 1: Make a database table with 2 fields: NAME TYPE onlineuser_session varchar(100) onlineuser_time varchar(30) Step 2: Download the dbal from my site: here. Step 3: Make these files db = $db; $this->table = $tblcounter; $this->sid = session_id(); $this->do_count(); } function do_count () { if ($this->is_logged()) $this->update_log(); else $this->new_log(); $sql = "SELECT COUNT(onlineuser_session) as total " . "FROM {$this->table} " . "WHERE onlineuser_time=’".(time()-60)."’"; $this->db->Query ($sql); $RS = $this->db->FirstRow(); $this->online = $RS["total"]; } function new_log () { $sql = "INSERT INTO {$this->table} " . "(onlineuser_session, onlineuser_time) " . "VALUES (‘{$this->sid}’,’".time()."’)"; $this->db->Query ($sql); } function update_log () { $sql = "UPDATE {$this->table} " . "SET onlineuser_time=’".time()."’ " . "WHERE onlineuser_session=’{$this->sid}’"; $this->db->Query ($sql); } function is_logged () { $sql = "SELECT * FROM {$this->table} " . "WHERE onlineuser_session=’{$this->sid}’"; $this->db->Query ($sql); if ($this->db->RowCount>0) return true; else return false; } }; ?> online; ?>