I finally found time to figure out a solution.
It is to simply store session information each time a user enters the site or logs in, then use that data to show the number of site hits...
Steps are as follows:
1. Create a new table for storing all historical session information (schema exactly the same as the '4images_sessions' table).
DROP TABLE IF EXISTS 4images_sessions_hist;
CREATE TABLE 4images_sessions_hist (
session_id varchar(32) NOT NULL default '',
session_user_id mediumint(8) NOT NULL default '0',
session_lastaction int(11) NOT NULL default '0',
session_location varchar(255) NOT NULL default '',
session_ip varchar(15) NOT NULL default '',
UNIQUE session_id_date (session_id, session_lastaction),
KEY session_id_ip_user_id (session_id,session_ip,session_user_id)
) TYPE=MyISAM;
2. Change includes/sessions.php
2.1 Find:
if (!$login_process) {
$this->session_id = $this->generate_session_id();
$sql = "INSERT INTO ".SESSIONS_TABLE."
(session_id, session_user_id, session_lastaction, session_location, session_ip)
VALUES
('$this->session_id', ".$this->user_info['user_id'].", $this->current_time, '$this->user_location', '$this->user_ip')";
$site_db->query($sql);
...
Add after:
$sql = "INSERT INTO ".SESSIONS_HIST_TABLE."
(session_id, session_user_id, session_lastaction, session_location, session_ip)
VALUES
('$this->session_id', ".$this->user_info['user_id'].", $this->current_time, '$this->user_location', '$this->user_ip')";
$site_db->query($sql);
2.2 Find:
if ($user_id != GUEST) {
if ($row[$user_table_fields['user_password']] == $user_password) {
$ip_sql = ($this->mode == "get") ? " AND session_ip = '$this->user_ip'" : "";
$sql = "UPDATE ".SESSIONS_TABLE."
SET session_user_id = $user_id
WHERE session_id = '$this->session_id'
$ip_sql";
$site_db->query($sql);
Add after:
$sql = "UPDATE ".SESSIONS_HIST_TABLE."
SET session_user_id = $user_id
WHERE session_id = '$this->session_id'
$ip_sql";
$site_db->query($sql);
3. Add to includes/constants.php
define('SESSIONS_HIST_TABLE', $table_prefix.'sessions_hist');
4. Now access the data, for example I will include total number of hits on your site. If you have a stats.php in the PHP root dir, add:
//-----------------------------------------------------
//--- total user sessions ----------------------------
//-----------------------------------------------------
$sql = "SELECT COUNT(*) as sessions
FROM ".SESSIONS_HIST_TABLE." sh, ".USERS_TABLE." u
WHERE sh.session_user_id = u.user_id
AND u.user_level <> ".ADMIN;
$row = $site_db->query_firstrow($sql);
$total_sessions = $row['sessions'];
$site_template->register_vars("total_sessions", $total_sessions);
unset($total_sessions);
5. Use the statistic in a page, i.e. using {total_sessions} somewhere
--- end of mod ---
There is one undesirable effect of logging sessions this way is that if a user logs off, and the screen refreshs back to the main page, as a Guest, then a
second session is logged even though it is the same user. Also, when you first go to the your site, sometimes when you reload the page another session is logged, but once you follow a link a reload no longer adds a new session.
Any ideas to fixing (or better understanding) these issues?
Cheers,
possom