Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Total number of active sessions

Posted on 2004-10-18
7
Medium Priority
?
1,622 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 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