pq Visitor Counter and Tracker

Script by pftq
Version 1.12
Do not modify or distribute original code without permission!
www.pftq.com/pqcounter/

Intro

     This was one of my first fully function mySQL and PHP based scripts, although it is not the first I released. I used it on my own site for many months before considering a release version. I've always hated how static and unchangable things were when I downloaded scripts. Every aspect of the counter I ever needed changing, I made it changable in the settings file. I hope it will be one of the most customizable and easy-to-use scripts you will have.

Install

The installation is very simple (only 3 steps). The script does all the work - you only have to upload it and create a new database for it. It still may seem foreign to those new to mySQL and PHP, so I will outline the 3 easy steps below.

1) SETTING UP MYSQL

The script uses mySQL to organize the stats and visitors. Before we do anything, please create a user and database for the files.

For those who do not know how to do this, find where your mySQL databases are located (cPanel is under "mySQL Databases"). Create a new database (cPanel > New Database). Create a user and password (cPanel > Create User). Then add the user to the database with all priviledges (cPanel > Add User to Database).

That is all for mySQL! You do not have to do anything else. When first run (included into another page), the script will take care of creatings its tables inserting everything properly.

2) CUSTOMIZING

Before we put it on the site, you just need to customize a few things (ex. mySQL inf). This is the fun part. :)

=== The files ===

The Actual Counter

The script is heavily customizable. Open up counter.php in Notepad and you will see everything arranged for you to edit. Read the comments and edit as you wish.

pqcounter_core.php is the actual working code - do not edit this.

Optional Visitor Tracker

index.php is the Visitor Tracker which you can see all visitors who have been on your site recent enough to be counted as the same visitor again (duration variable). Simply open this file in your web browser to use.

tracker.php is the code and layout that runs this. Edit as you wish but be careful not to affect the actual code and break it. However, it will not hurt the actual counter.

3) INSTALLATION

To install, simply upload the PHP files to a folder on your site. A folder is not necessary, but keeps things organized; all files must, however, be in the same folder.

You should have four files: counter.php, pqcounter_core.php, tracker.php, index.php

Congrats! You are done and the script is fully functional!

//////////////////// COUNTING PAGES ////////////////////////

For every page you wish to count, use PHP to include the counter.php file at the top.

PHP Beginners: Just put <?php include("path to counter (non http)"); ?> at top, replacing the text with the path of course.

Example: If the page was in the same folder as the counter, you'd put <?php include("counter.php"); ?>

The page will be counted and you can see all visitors to that page on the visitor counter mentioned above (index.php in webbrowser).

//////////////// SHOWING STATS AND MORE! ///////////////////

Below is an 'attempt' to explain all variables. It is much easier to understand however, by seeing the "Demo/Example of Stats" page available at: www.pftq.com/pqcounter/

Below are all the variables you can use in your page. Place the variable on your page where you want the stat to show up. Read the description for more details.

==== SETTINGS VARIABLES =====

The below settings are used in index.php; see that file in Notepad for examples and notes.

$discount_counter determines whether to count a page. Set it to true BEFORE including the counter file on your page to NOT count it in the stats.

Example: <?php $discount_counter=true; include("counter.php"); ?>

For the tracker: Similarly, you can also specify $pqhidden_pages to show which pages to not show. See the index.php file for example.

===== OUTPUT VARIABLES ======

PHP Beginners: Always enclose the variables in <?php echo VARIABLE; ?>
Example for total visitors: <?php echo stat_("total"); ?>

ALL settings you editted above are ALSO usable! Example: <?php echo $timezone; ?> to display timezone.
Use $activenum in your page to return the current number of visitors online.
Example: <?php echo $activenum; ?>

Use the stat_("Field") function to return stats. Fields can be:
total - Total visitors
total_hits - Total Visits activecount - Highest Active Visitor Count
activetime - Time of Highest Vistors
old_activecount - Last Record of Highest Visitors
old_activetime - Last Time of Highest Visitors
todaycount - Visits today
ydaycount - Visits yesterday
maxdayct - Highest visits in one day
maxday - Day of highest visitors
monthcount - Visits this month
ymonthcount - Visits last month
maxmonthct - Highest visits in one month
maxmonth - Month of highest visits
Example of highest visitors: <?php echo $stat_['activecount']; ?>

$varip is the ip address of the user.
$fileloc is the current url position (starts with / as root).

==== CONDITION VARIABLES ====

The variables can be checked to determine information about the visitor. For example, you might want a welcome message for newcomers and a "Hello again" message to returns (visits).

PHP Beginners: Enclose the variable in <?php if(VARIABLE===true) { ?> EFFECT THAT HAPPENS IF TRUE <?php } ?>. It can be true or false.
Example: If visitor is not new, say welcome back: <?php if($newvisitor===false) { ?> Welcome back!<?php } ?>

$newvisit - returns true if the visitor made a new visit.
$newvisitor - returns true if visitor is entirely new.

PHP Beginners tip: Conditions can be combined with && (and) or || (or).
Example: <?php if($newvisit===true&&$newvisitor===false) { ?>
--------- Now the condition checks to make sure it is a new VISIT AND that it isn't a new VISITOR; it makes sure it's a return visitor.

==== MYSQL CONNECTIONS (Advanced Users) ============

The mysql connection you specified in the settings can also be connected to with the following function:
connect_countersql();

I hope you find good use of the script. Enjoy!

If you need help, feel free to ask in the forums at

www.pftq.com

Please do not distribute the files to anyone else without permission. Refer them to the site.

Thanks!

pftq
www.pftq.com


*Updated February 8 2007, 1.01: More mySQL features/settings added. IP-Lookup feature added.
*Updated February 10 2007, 1.02: Added setting to hide pages in tracker (ex. if public view)
*Updated February 12 2007, 1.03: Fixed bug where Tracker of "Visits Today" do not reset if site not visited over a day.
*Updated February 19 2007, 1.04: Fixed PHP warnings from robots who have incomplete referer data.
*Updated February 20 2007, 1.05: Enabled filtering for Visitors Today.
*Updated March 3 2007, 1.05: Enabled day/monthly counts to go by set timezone (not 0 GMT); tracker has "Limit" features that determine how many visitors to currently view (day, week, month, or no limit).
*Updated March 4 2007, 1.05: Fixed yesterday count bug created in 1.05.
*Updated August 1 2007, 1.06: Fixed bug with hidden pages not being hidden. Unreleased until further updates.
*Updated August 29 2007, 1.06: Fixed critical bug where last highest visitor count does not update the time. Fixed several redundancies with timezones; record times no longer change with timezone changes.
*Updated August 31 2007, 1.07: Banned IPs now allow '*' wildcard (no quotes) and no longer have to be a certain length (ban "*" and you ban all).
*Updated September 9 2007 1.07: Included a 'kill-url' feature, which kills a page if the url contains certain phrases; useful if you want to stop hacking attempts and know the url they try to insert.
*Updated Febr. 3 2008 1.07: New $fileloc variable holds current visitor url location. Total visits counting added. Allow tables to be custom-named to let multiple counters exist in same site. Cleaned up some redundent code. Also added new cross-domain counting/tracking. This allows the counter/tracker to work across multiple domains/subdomains. All it takes is to enable it in the settings.
*Updated Mar. 29 2008 1.08: Fixed timezone bug in tracker file (timezone ignored). Added '$badips' setting to block specific IPs when detected. Added setting to hide IPs on the tracker (if you were to show the page publicly).
*Updated Apr. 29 2010 1.08: Checked ip against server's ip due to some servers being misconfigured and returning its own IP instead of the visitor's.
*Updated Jul. 18 2010 1.09: Does not count or record the visitor if the IP given is blank; can happen if running scripts on the server but no one actually visiting the site.
*Updated Aug. 8 2010 1.09: Fix bad referer setting that I implemented earlier but forgot to finish.
*Updated Nov. 17 2010 1.09: Corrected implementation of time to account for server timezone settings (originally just assumed server was at GMT). All times input into database are now GMT.
*Updated Dec. 2 2010 1.10: Added $timeoffset variable for cases where server's GMT setting might be off and need manual correcting.
*Updated Dec. 5 2010 1.10: Added $forceLimit setting to disable "No Limit" view on Tracker (prevent server resource abuse). Disabled counting visitors banned via IP/Referer/URL and grouped them with bots; highlighted them in red and disabled publishing link to referers to discourage linking to their websites.
*Updated Jan. 6 2012 1.11: Added a $maxvisitors variable to force pages to quit if a certain number of visitors online at once (example, sudden spam attack of 1000 bots). Also added $badentries for blocking user based on his first page; spambots like to go register first thing, for example. Fixed numerous logs being entered if the user has no IP or location (doesn't add user to the log).
*Updated Jan. 20 2012 1.11: Changed default ip lookup to http://whois.domaintools.com/ as the old URL no longer worked.
*Updated Feb. 24 2015: Added $ip_override variable for other scripts to override the ip detection.
*Updated Aug. 6 2015 1.12: Added bot detection via user agent and $botAgents setting. Also added helper function "uncountedIP()" for anyone to use for detecting bots.
*Updated Dec. 31 2019 1.12: Added htmlentities wrapper to referrals/agent strings on IP viewing page to prevent malicious code stored there from running in Javascript.