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! |
Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success. FREE! Go There Now!
|
|
|
|
Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
|
|
|
|
You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
|
|
|
|
XML has become a common way of storing business data as flat files and many data server vendors including IBM have provided ways to store this data within relational database systems. Increasingly collections of XML files are accessed like databases using an xQuery and other XML standard mechanisms. Businesses find the need to combine the traditional tabular structured data with XML formatted data. In this webcast, you’ll learn about IBM’s WebSphere Federation Server technology, which provides users with the ability to integrate these two data formats. FREE! Go There Now!
|
|
|
|
Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing. FREE! Go There Now!
|
|
|
|
This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |