• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 46
  • Last Modified:

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

0
k412m4z
Asked:
k412m4z
  • 5
  • 2
1 Solution
 
Ray PaseurCommented:
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();
0
 
k412m4zAuthor Commented:
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
0
 
Ray PaseurCommented:
No, there is not a ton of effort at all, but you might want to get a PHP programmer to help you.  It's only a few lines of code.  My IP address is in this array and when I run this script it says, I AM IGNORING YOU.
<?php // RAY_temp_k412.php
error_reporting(E_ALL);


// SKIP CERTAIN IP ADDRESSES


// ADD YOUR IP ADDRESS TO THIS ARRAY
$ips = array
( '98.169.58.122'
, '69.65.102.255'
)
;

// IF THE CURRENT IP ADDRESS IS IN THE ARRAY, SEND THIS MESSAGE
if (in_array($_SERVER["REMOTE_ADDR"], $ips)) echo "I AM IGNORING YOU";

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
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
0
 
Hugh McCurdyCommented:
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.
1
 
k412m4zAuthor Commented:
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..
0
 
Ray PaseurCommented:
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?
0
 
Ray PaseurCommented:
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now