Hey guys,
I need help on the script below (I already have the MySQL table structure and whatnot, I just need to figure out how to make this work with my network):
Okay, so I have a network - it is made up of about 12 diff sites. How would I make this script work? This script is made to count the people online one specific site, and then how many on the rest of the other sites (the network) and show both statistics.
Would I add my network sites to this area of the code, and make it look like this - or am I wrong? :
// Allowing other sites in on the act (via query string)
// Follows pattern:
// mysql_store_name => html_display_name
$sites = array (
'site1' => 'www.site1.com',
'site2' => 'www.site2.com',
'site3' => 'www.site3.com',
'site4' => 'www.site4.com',
'site5' => 'www.site5.com',
'site6' => 'www.site6.com',
(etc, etc, until I go to site 12)
I'm really confused on how to set this up and how to have it work with each domain on my network using only one installation..
Please help,
Thanks,
Andy
I need help on the script below (I already have the MySQL table structure and whatnot, I just need to figure out how to make this work with my network):
Code:
<?php
/**
* A simple visitor tracking script for use on multiple sites (a network).
*
* Copyright 2004 Peter Cowburn <[email protected]>
*
* Examples:
* [url]http://logohq.net/misc/code/php_counter/[/url]
* [url]http://logohq.net/misc/code/php_counter/?site=mysite[/url]
* [url]http://logohq.net/misc/code/php_counter/?site=other[/url]
*
* Any additions/modifications/distribution of this material is
* allowed without contacting the original author or crediting of the
* original author.
*/
/**
* A simple error handling function
*
* @param string $str Message to be displayed.
* @param string/integer $line (Optional) Line number of error.
* @return void Prints error message and halts all processing.
*/
function mysql_die($str, $line=null) {
$friendly = 'An error has occured and all page processing has been halted.';
if ($line !== null) {
$str = 'Line ' . $line . ': ' . $str;
}
$msg = sprintf('<p>%s</p><code>%s</code>', $friendly, $str);
die ($msg);
}
// holder for mysql preferences
// *** CHANGE THESE! ***
$mysql = array (
'host' => 'mysql.mynetwork.com', // or, localhost?..
'user' => 'username',
'pass' => 'password',
'db' => 'dbCounter',
'table' => 'tCount'
);
// holder for our visitor's data
$data = array (
'ip' => $HTTP_SERVER_VARS['REMOTE_ADDR'],
);
// Information for "default" hits
$default_site = array (
'key' => 'yaxay',
'name' => 'yaXay.com'
);
// Allowing other sites in on the act (via query string)
// Follows pattern:
// mysql_store_name => html_display_name
$sites = array (
'mysite' => 'www.MySite.com',
'other' => 'www.MyOtherSite.com'
);
if (($_GET['site'] == '') || (array_key_exists($_GET['site'], $sites) === false)) {
$site_key = $default_site['key'];
$site_name = $default_site['name'];
} else {
$site_key = preg_replace('#[^a-zA-Z0-9_]+#', '', $_GET['site']);
$site_name = $sites[$site_key];
}
// holder for general preferences
$prefs = array (
// how long do we recognise a "visitor" for? (minutes)
'timeout' => 15,
// site name in the DB
'tSite' => $site_key,
// Friendly site name
'fSite' => $site_name,
// Friendly network name
'fNet' => 'MyNetwork',
// Friendly message
'fMess' => '<p>Visitor count for %s: %d</p>',
// Show friendly messages? (true/false)
'showMessages' => true
);
// Holder for our visitor counts
$count = array (
'site' => 0,
'net' => 0
);
// Connect to host and DB
$conn = mysql_connect($mysql['host'], $mysql['user'], $mysql['pass'])
or mysql_die('Cannot connect to database host.', __LINE__);
$db = mysql_select_db($mysql['db'], $conn)
or mysql_die('Could not select database to retrieve data.', __LINE__);
// Check if the user has already been logged and is still "active"
$sql = sprintf("SELECT * FROM %s WHERE ip='%s' AND site='%s' AND DATE_SUB(NOW(),INTERVAL %s MINUTE)<=stamp;", $mysql['table'], $data['ip'], $prefs['tSite'], $prefs['timeout']);
$result = mysql_query($sql, $conn) or mysql_die('Could not check visitor table.', __LINE__);
if (mysql_num_rows($result) > 0) {
$record = mysql_fetch_object($result);
// Visitor already here... so update to current time
$sql = sprintf("UPDATE %s SET stamp=NOW() WHERE site='%s' AND ip='%s' AND stamp='%s';", $mysql['table'], $prefs['tSite'], $data['ip'], $record->stamp);
mysql_query($sql, $conn) or mysql_die('Could not update timestamp.', __LINE__);
} else {
// A new visitor, so lets add them
$sql = sprintf("INSERT INTO %s SET ip='%s', site='%s';", $mysql['table'], $data['ip'], $prefs['tSite']);
mysql_query($sql, $conn) or mysql_die('Could not log visitor.', __LINE__);
}
// Now lets count our active users on the site...
$sql = sprintf("SELECT COUNT(ip) as total FROM %s WHERE site='%s' AND DATE_SUB(NOW(),INTERVAL %s MINUTE)<=stamp;", $mysql['table'], $prefs['tSite'], $prefs['timeout']);
$result = mysql_query($sql, $conn) or mysql_die('Error gathering local stats.', __LINE__);
$count['site'] = mysql_result($result, 0);
// ... and on the entire network!
$sql = sprintf("SELECT COUNT(ip) as total FROM %s WHERE DATE_SUB(NOW(),INTERVAL %s MINUTE)<=stamp;", $mysql['table'], $prefs['timeout']);
$result = mysql_query($sql, $conn) or mysql_die('Error gathering network stats.', __LINE__);
$count['network'] = mysql_result($result, 0);
// If we chose to show the counts
if ($prefs['showMessages'] == true) {
// Tell the world what we have found out!
printf($prefs['fMess'], $prefs['fSite'], $count['site']);
printf($prefs['fMess'], $prefs['fNet'], $count['network']);
}
// Don't forget to close the DB connection
@mysql_close($conn);
// And finish.
exit;
?>
Okay, so I have a network - it is made up of about 12 diff sites. How would I make this script work? This script is made to count the people online one specific site, and then how many on the rest of the other sites (the network) and show both statistics.
Would I add my network sites to this area of the code, and make it look like this - or am I wrong? :
// Allowing other sites in on the act (via query string)
// Follows pattern:
// mysql_store_name => html_display_name
$sites = array (
'site1' => 'www.site1.com',
'site2' => 'www.site2.com',
'site3' => 'www.site3.com',
'site4' => 'www.site4.com',
'site5' => 'www.site5.com',
'site6' => 'www.site6.com',
(etc, etc, until I go to site 12)
I'm really confused on how to set this up and how to have it work with each domain on my network using only one installation..
Please help,
Thanks,
Andy














