| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| Senior Member | Quick question on customizing this script.. 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): Code: <?php /** * A simple visitor tracking script for use on multiple sites (a network). * * Copyright 2004 Peter Cowburn <peter@cowburn.info> * * Examples: * http://logohq.net/misc/code/php_counter/ * http://logohq.net/misc/code/php_counter/?site=mysite * http://logohq.net/misc/code/php_counter/?site=other * * 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; ?> 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 |
| |
| | #3 (permalink) |
| Pro Coder & Designer | are they all located under 1 server at the same IP or same 'reseller account' ?
__________________ Online Dragonball Game |
| |
| | #6 (permalink) |
| Senior Member | I'm actually trying to figure out the same thing, i only have 2 sites on one account though..If you get this script working let me know..I would love to have what you are trying to accomplish.
__________________ RB's Lingerie COME VISIT NOW!!! Sign up for adsense at www.ratemymug.com This line is for sale PM for details |
| |
| | #7 (permalink) |
| NamePros Member | Can you please explain what exactly isn't working? I scanned through your code, but without spending a lot of time on it, it's tough to see where the trouble is. Can you try to make your question more specific or post a much simpler script to illustrate what's not working? Cheers! |
| |
| | #8 (permalink) |
| Senior Member | It's mainly just the setting-it-up part that I am confused about. I have 12 different sites in my network and I want them all to use this coding and for it to say how many are on one of those 12 sites and how many are on all of the sites put together, I hope that kind of makes sense :$ |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 60.000 Templates, scripts, fonts, banners etc. $9.95 | atkims | Web Development Wanted | 19 | 11-16-2004 09:48 AM |
| Domain Rating Script for Sale : $200.00 - only 10 scripts will be sold | fonzerelli_79 | Scripts For Sale | 3 | 08-31-2003 08:41 AM |