Thursday, October 11, 2007

Using Sessions in PHP

Introduction Building web applications with membership management is one of the most frequent tasks that every programmer does. Managing membership data, such as username, password and the member's profile with sessions in PHP is the easiest and simplest solution, although it is not the only one. Preparation With this tutorial, I assume you are using PHP 4.1.0 or the later. The first thing that we should know to use sessions is that you have to initialize the session. session_start(); Any script which has that line would make the script available to register a new session or read an existing session which weÂ’ve defined on another page. Enough of the blah blah stuff, shall we now go to the example stuff?

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 .= "
  • Your password is empty.
  • "; /* from here, do your sql query to query the database to search for existing record with correct username and password */ if (trim($error)!="") return $error; } function login ($forms) { $username = $forms["username"]; $password = $forms["password"]; /* do your sql query again, but now returning the id of member */ return $member_id; } ?>
    Username :
    Password :
    In the above example, we have built three pages. The first page is functions.php. In this page we build all the functions to do login checking, the login and the login detector. The second page is login.php. We will show the login form to our user and do some processes to register sessions when they have passed the login check. Third and the last page is the sample of how to use the system. This page is only available when user has logged in or has the session variable "member_id" with some value, not null.

    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; ?>

    No comments: