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.
Main Topics
Browse All TopicsIs 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
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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_ma
function open($session_path, $session_name) {
mssql_pconnect("HOSTNAME",
or die("Can't connect to mssql server! ");
mssql_select_db("[My-Datab
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
$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("
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.
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.
Business Accounts
Answer for Membership
by: RoonaanPosted on 2004-10-19 at 00:36:38ID: 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-