Link to home
Start Free TrialLog in
Avatar of k412m4z
k412m4z

asked on

Exclude my IP from this page counter script

I am using this open source script, and would like to adjust it so that I don't see pageview counts go up whenever I view the different pages on my site.. This way, I can see exactly where interest is, and know my personal viewing of certain things isn't skewing the data.

I've attached the script I am using... If there is a way to block 'multiple' selected IPs that would be nice too, since I work with a couple colleagues.

Any help would be appreciated! Thanks


<?php
/*******************************************************************************
*  Title: PHP hit counter (PHPcount)
*  Version: 1.3 @ August 21, 2009
*  Author: Klemen Stirn
*  Website: http://www.phpjunkyard.com
********************************************************************************
*  COPYRIGHT NOTICE
*  Copyright 2004-2009 Klemen Stirn. All Rights Reserved.
*
*  This script may be used and modified free of charge by anyone
*  AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT.
*  By using this code you agree to indemnify Klemen Stirn from any
*  liability that might arise from it's use.
*
*  Selling the code for this program, in part or full, without prior
*  written consent is expressly forbidden.
*
*  Obtain permission before redistributing this software over the Internet
*  or in any other medium. In all cases copyright and header must remain
*  intact. This Copyright is in full effect in any country that has
*  International Trade Agreements with the United States of America or
*  with the European Union.
*******************************************************************************/

// SETUP YOUR COUNTER
// Detailed information found in the readme.htm file

// Count UNIQUE visitors ONLY? 1 = YES, 0 = NO
$count_unique = 0;

// Number of hours a visitor is considered as "unique"
$unique_hours = 1;

// Minimum number of digits shown (zero-padding). Set to 0 to disable.
$min_digits = 0;

#############################
#     DO NOT EDIT BELOW     #
#############################

/* Turn error notices off */
error_reporting(E_ALL ^ E_NOTICE);

/* Get page and log file names */
$page = input($_GET['page']) or die('ERROR: Missing page ID');
$logfile = 'logs/' . $page . '.txt';

/* Does the log exist? */
if (file_exists($logfile))
{
	/* Get current count */
	$count = intval(trim(file_get_contents($logfile))) or $count = 0;
	$cname = 'tcount_unique_'.$page;

	if ($count_unique==0 || !isset($_COOKIE[$cname]))
    {
		/* Increase the count by 1 */
		$count = $count + 1;
		$fp = @fopen($logfile,'w+') or die('ERROR: Can\'t write to the log file ('.$logfile.'), please make sure this file exists and is CHMOD to 666 (rw-rw-rw-)!');
		flock($fp, LOCK_EX);
		fputs($fp, $count);
		flock($fp, LOCK_UN);
		fclose($fp);

		/* Print the Cookie and P3P compact privacy policy */
		header('P3P: CP="NOI NID"');
		setcookie($cname, 1, time()+60*60*$unique_hours);
	}

	/* Is zero-padding enabled? */
    if ($min_digits > 0)
    {
	    $count = sprintf('%0'.$min_digits.'s',$count);
    }

    /* Print out Javascript code and exit */
    echo 'document.write(\''.$count.'\');';
    exit();
}
else
{
    die('ERROR: Invalid log file!');
}

/* This functin handles input parameters making sure nothing dangerous is passed in */
function input($in)
{
    $out = htmlentities(stripslashes($in));
    $out = str_replace(array('/','\\'), '', $out);
    return $out;
}
?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

After line 44 you can make a test for the contents of $_SERVER["REMOTE_ADDR"] to see if it is equal to your IP address.  If you put a collection of IP addresses in an array you can use an iterator like foreach() to walk the array and compare each hard-coded IP address to the contents of the server variable.

You can run this script (shown here in its entirety) and look near the bottom to find the way that PHP encodes your IP address.

<?php phpinfo();
Avatar of k412m4z
k412m4z

ASKER

Thanks for chiming in! But, you're talking way too technical for me... lol

PHP knowledge: zero

If there's a ton of effort in this, I'll just skip on it. It was something to just be more a convenience than anything.

Cheers
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But that said...

Why not get rid of the hit counter and use the industry-standard Google Analytics?  It's widely respected and very well-understood.  Just a thought, ~Ray
I like Ray's script.  Just be sure to change it to use your IP.

Basic PHP isn't hard to learn.     http://www.w3schools.com/php/    can get you started.    Started enough so that you can understand Ray's code

There are lots of PHP experts here if you would like to learn PHP and have questions.
Avatar of k412m4z

ASKER

Added that data after line 44, and basically what's happening is the view count on the page isn't showing up, but it's still counting each time I refresh (given that I check the text file and see the number going up)

Hmm..
This could be a bit of a handicap if you want to work with PHP: PHP knowledge: zero.

Suggestion: Post the code as it exists now and post your IP address.  I'll try to modify the code for you.

You have a fixed IP address, right?
The EE_AutoDeleter does not see capable of making correct judgements about Experts-Exchange answers!

The correct answer, including a tested and working code example, is at https:#a36931292.