A function that will provide counting for an unlimited amount of pages. Checks IPs and won't up count if the same IP hits the same page over and over.
By : Matt
<?
function Counter($page = '') {
/* this function will take $page as an arguement.
* using different values for $page will allow
* you to use a different counter on each page.
* if you want the same counter for all of your
* site then don't pass in $page */
/* connect to and select database */
mysql_connect ('localhost','username','password');
mysql_select_db ('database');
/* set expire time for ips
* 600 seconds = 10 minutes */
$expire = time() - 600;
/* delete from table where ip has expired */
$query = "DELETE FROM counter_ips " .
"WHERE viewtime < $expire " .
"AND page = '$page'";
mysql_query ($query);
/* now we check to see if the users
* ip exists in the table */
$query = "SELECT ip FROM counter_ips " .
"WHERE ip = '{$_SERVER['REMOTE_ADDR']}'" .
"AND page = '$page'";
$result = mysql_query ($query);
if (mysql_num_rows ($result) > 0) {
/* if the ip existed, update the view time */
$viewtime = time();
$query = "UPDATE counter_ips SET viewtime = '$viewtime'" .
"WHERE ip = '{$_SERVER['REMOTE_ADDR']}'" .
"AND page = '$page'";
mysql_query ($query);
} else {
/* ok time to record a hit and the ip */
/* first check if a record exists for $page */
$query = "SELECT num FROM counter " .
"WHERE page = '$page'";
$result = mysql_query ($query);
if (mysql_num_rows ($result) == 0) {
/* if no row, add one */
$query = "INSERT INTO counter VALUES " .
"(1,'$page')";
mysql_query ($query);
} else {
/* there is a row so just update it */
$query = "UPDATE counter SET num = num + 1 " .
"WHERE page = '$page'";
mysql_query ($query);
}
/* add ip to ip table */
$viewtime = time();
$query = "INSERT INTO counter_ips VALUES " .
"('{$_SERVER['REMOTE_ADDR']}', '$page', $viewtime)";
mysql_query ($query);
}
/* now we get the counter value and return it */
$query = "SELECT num FROM counter WHERE page = '$page'";
$row = mysql_fetch_assoc (mysql_query ($query));
return $row['num'];
}
/* table dumps
CREATE TABLE `counter` (
`num` int(10) unsigned NOT NULL default '0',
`page` varchar(100) default NULL,
KEY `page` (`page`)
) TYPE=MyISAM;
CREATE TABLE `counter_ips` (
`ip` varchar(16) NOT NULL default '',
`page` varchar(100) default NULL,
`viewtime` int(11) unsigned NOT NULL default '0',
KEY `ip` (`ip`),
KEY `viewtime` (`viewtime`),
KEY `page` (`page`)
) TYPE=MyISAM;
*/
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Counters Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br /> FREE! Go There Now!
|
|
|
|
You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download the latest trial version of IBM Data Studio V1.1 at no cost. IBM Data Studio is a comprehensive data management solution that helps you effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life cycle utilizing a consistent and integrated user interface. Unlike other client-side data management solutions that focus on only one aspect of the application lifecycle or database administration, Data Studio complements the Rational Software Delivery platform, providing unparalleled flexibility for a heterogeneous data server environment across platforms. FREE! Go There Now!
|
|
|
|
Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
|
|
|
|
Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered! FREE! Go There Now!
|
|
|
|
Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually. FREE! Go There Now!
|
|
|
|
Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
|
|
|
|
Join this webcast to learn how IBM Rational's Functional Testing solution enables you to implement automation your way, at your pace, with your existing staff. In this webcast, you’ll learn how you can eliminate redundancy of manual test scripts, reduce errors, and increase test coverage through test automation. After this presentation you will understand how IBM Rational Functional Testing solution can streamline your manual testing and make test automation easily attainable. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |