Solved

Total number of active sessions

Posted on 2004-10-18
7
1,602 Views
Last Modified: 2008-01-09
Is it possible to get the total number of active sessions for my website? I want to do this because I would like to see how many "guests" are on without having to add every single one to a database
0
Comment
Question by:LiquidIce911
  • 3
  • 2
7 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 12345216
Well as security issues are often telling you to have a custom session handler it would be easy to count files or db-records as you will. This only is more cumbersome than just inserting hits into your database and selecting unique ip's.

Regards

-r-
0
 
LVL 1

Author Comment

by:LiquidIce911
ID: 12350602
If there is no way of getting the number of sessions what is the fastest way about handling this because my site may have up to 10,000 active users.
0
 
LVL 1

Accepted Solution

by:
maunded earned 100 total points
ID: 12446158
You might need to write a custom session handler to insert the session data into a database.  I have just moved our site from using file based sessions to DB based for pretty much the same reason as yours.  Writing the session handler only took a couple of hours :)
Try this:
<?php

/* Create new object of class */


$sql_table = "Session_Table";

$lifetime = get_cfg_var("session.gc_maxlifetime");

function open($session_path, $session_name) {
      
      mssql_pconnect("HOSTNAME", "USERNAME", "PASSWORD")
      or die("Can't connect to mssql server! ");
      
      mssql_select_db("[My-Database]")
      or die("Can't select mssql sessions database");
      return (true);
}

function close() {
      
      return true;
      
}

/* Read session data from database */
function read($ses_id) {
      global $sql_table;
      $session_sql = "SELECT ses_value FROM " . $sql_table
      . " WHERE ses_id = '$ses_id'";
      $session_res = mssql_query($session_sql);
      $array = mssql_fetch_array($session_res);
      $ses_data = $array['0'];
      if ($session_res = "") {
            return '';
      }
      else return $ses_data;
}

/* Write new data to database */
function write($ses_id, $ses_data) {
      global $sql_table;
      $find_id_query = "SELECT ses_id from " . $sql_table . " WHERE ses_id = '$ses_id'";
      $result = mssql_query($find_id_query);
      $rows = mssql_num_rows($result);
      if ($rows >= 1) {
            $session_sql = "UPDATE " . $sql_table
            . " SET ses_time='" . time()
            . "', ses_value='$ses_data' WHERE ses_id='$ses_id'";
            $session_res = mssql_query ($session_sql);
            if (!$session_res) {
                  return FALSE;
            }
            elseif ($session_res == "1") {
                  return TRUE;
            }
      }
      else {
            $session_sql = "INSERT INTO " . $sql_table
            . " (ses_id, ses_time, ses_start, ses_value)"
            . " VALUES ('$ses_id', '" . time()
            . "', '" . time() . "', '$ses_data')";
            $session_res = mssql_query ($session_sql);
            if ($session_res == "1") {
            return (true);
            }
            else {
            return (false);
            }
      };
}            
            

/* Destroy session record in database */
function destroy($ses_id) {
      global $sql_table;
      $session_sql = "DELETE FROM " . $sql_table
      . " WHERE ses_id = '$ses_id'";
      $session_res = mssql_query ($session_sql);
      if (!$session_res) {
            return FALSE;
      }         else {
            return TRUE;
      }
}

/* Garbage collection, deletes old sessions */
function garbage($life) {
      global $sql_table;
      global $lifetime;
      $timenow = time();
      $ses_life = $timenow - $lifetime;
    $session_sql = "DELETE FROM " . $sql_table
                     . " WHERE ses_time < $ses_life";
      $session_res = mssql_query ($session_sql);
      if (!$session_res) {
            return FALSE;
      }         else {
            return TRUE;
      }
}

session_set_save_handler("open","close","read","write","destroy","garbage");
session_start();

This is for mssql so change accordingly if you use MySQL.
I saved this file as session.inc then used require_once in the main section of the site.
Works like a charm!
Hope this helps.
D.

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:maunded
ID: 12446165
Sorry, forgot to add you need to change session handler in the php.ini from files to user (Took me a while to figure out why my sessions wernt going into the db :|)
0
 
LVL 1

Author Comment

by:LiquidIce911
ID: 12450366
I didnt want to write my own session handler but this raises another question .. is writing a SQL session handler faster than the standard file handler? My site will have thousands of users at a time and I would like to maximize as much speed as possible.
0
 
LVL 1

Expert Comment

by:maunded
ID: 12450442
I guess you could always count the number of sess_* files in the session directory(/tmp).
:)
Im not sure about he speed, I used the sql session handler because I load balance my site across a few servers, and having file sessions just dosent work the way I want it to without using networked storage, which would be slower than a db query.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question