Simple Machines Community Forum

Customizing SMF => Modifications and Packages => Mod Requests => Topic started by: Harro on June 01, 2005, 07:18:42 PM

Title: unique hits
Post by: Harro on June 01, 2005, 07:18:42 PM
Didn't realy know where to put this one, so if it's the wrong place, feel free to move :p

Anyway, on my old phpbb forum I had a small mod installed that displayed the number of unique hits in the last xx hours (24 by default).
Was wondering if anybody could convert that mod to smf.
The number was displayed on the bottom left, even under the copyright and stuff.
Here is that modification:

#################################################################
## MOD Title: Unique Hits MOD
## MOD Version: 1.0
## MOD Author: powerboy <[email protected]> http://www.rarextreme.com
## MOD Description: MOD to show the number of unique hits to your phpBB 2
##                  during a specified time period.  Default is 24 hours.
##
## Installation Level: Moderate
## Installation Time: 10 Minutes
## Files To Edit: constants.php, functions.php, page_tail.php, overall_footer.tpl
## Included Files: None
##
## Author Note:
## 1. This MOD requires the addition of 1 new table to your database.
##    Run the SQL statement below to add the table.
##    Change the `phpbb` prefix if you use another.
##
##    CREATE TABLE `phpbb_unique_hits` (`user_ip` CHAR(8) DEFAULT '0' NOT NULL, `time` INT(11) DEFAULT '0' NOT NULL, INDEX (`user_ip`)) TYPE = MYISAM;
##               
## 2. Change the overall_footer.tpl file for all your installed templates.
##
#################################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD
#################################################################

#
#-----[ OPEN ]------------------------------------------
#

includes/constants.php

#
#-----[ FIND ]------------------------------------------
#

?>

#
#-----[ ADD BEFORE ]------------------------------------------
#

define('UNIQUE_HITS_TABLE', $table_prefix.'unique_hits');

#
#-----[ OPEN ]------------------------------------------
#

includes/functions.php

#
#-----[ FIND ]------------------------------------------
#

?>

#
#-----[ ADD BEFORE ]------------------------------------------
#

//
// Checks the unique hits table and adds a user if they haven't visited
// the forums during the past time interval, defined as $expires.
//
// Returns the number of unique hits during the time interval.
//
// [email protected]
//

function uniquehits() {

global $db, $user_ip;

$expires = "86400"; // How many seconds to record unique hits for
                    // Change the time in page_tail.php to match
                    // Default is 24 hours (86400 seconds)

// Purge expired hits from the table
$sql = "DELETE FROM " . UNIQUE_HITS_TABLE . " WHERE time < " . (time()-$expires);
if( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not delete unique hits', '', __LINE__, __FILE__, $sql);
}

// First check to see if this $user_ip exists in the table
$sql = "SELECT user_ip FROM " . UNIQUE_HITS_TABLE . " WHERE user_ip='" . $user_ip . "'";
        if( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not check unique hits', '', __LINE__, __FILE__, $sql);
}

// If the $user_ip doesn't exists then add it to the table as another unique hit
if ( !($row = $db->sql_fetchrow($result)) )
{
// Insert the unique hit
$sql = "INSERT INTO " . UNIQUE_HITS_TABLE . "
(user_ip, time)
VALUES ('$user_ip', " . time() . ")";
if( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not update unique hits', '', __LINE__, __FILE__, $sql);
}
}

// Get the number of rows in the table (unique hits)
$sql = "SELECT user_ip FROM " . UNIQUE_HITS_TABLE;

        if( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not obtain unique hits', '', __LINE__, __FILE__, $sql);
}

        return $db->sql_numrows($result);
}

#
#-----[ OPEN ]------------------------------------------
#

includes/page_tail.php

#
#-----[ FIND ]------------------------------------------
#

$admin_link = ( $userdata['user_level'] == ADMIN ) ? '<a href="' . append_sid("admin/index.$phpEx") . '">' . $lang['Admin_panel'] . '</a><br /><br />' : '';

#
#-----[ ADD BEFORE ]------------------------------------------
#

//Unique Hits Mod
$unique_hits = '<br /><span class="copyright">There have been ' . uniquehits() . ' unique hit(s) in the past 24 hours.</span>';

#
#-----[ FIND ]------------------------------------------
#

$template->pparse('overall_footer');

#
#-----[ ADD BEFORE ]------------------------------------------
#

$template->assign_vars(array(
"UNIQUE_HITS" => $unique_hits)
);

#
#-----[ OPEN ]------------------------------------------
#

templates/subSilver/overall_footer.tpl

#
#-----[ FIND ]------------------------------------------
#

</div>

#
#-----[ ADD AFTER ]------------------------------------------
#

{UNIQUE_HITS}

#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM



Anybody able to do this for SMF?
Title: Re: unique hits
Post by: Ben_S on June 01, 2005, 07:24:41 PM
Enable the track stats and hits option in edit features and options. The data will then be shown on the statistics page, you could fairly easilly display it on the boardindex too.
Title: Re: unique hits
Post by: Harro on June 01, 2005, 07:28:44 PM
You mean the hits displayed in the stats?
They're not unique.
Title: Re: unique hits
Post by: Ben_S on June 01, 2005, 07:29:40 PM
They are page views, what do you mean by unique hits, do you mean unique visitors?
Title: Re: unique hits
Post by: Harro on June 01, 2005, 07:33:06 PM
Yeah, that was what I meant.
Sorry for the confusion :)