Solved

Total number of active sessions

Posted on 2004-10-18
7
1,614 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 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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