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! |
David Barnes, Lead Evangelist for IBM Emerging Internet Technologies 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!
|
|
|
|
Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. 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!
|
|
|
|
Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle. FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
|
|
|
|
As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
|
|
|
|
The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |