Page 1 of 1

Running PHPcount invisibly

Posted: Mon Sep 05, 2011 8:11 pm
by Andrew P.
I got PHPcount (the text counter) running satisfactorily fairly quickly, but since it insists on always displaying the count, it didn't completely meet my needs. I was looking for a site hit counter that would register a visitor regardless of how they arrive at the site, but I didn't want to be displaying the visitor count on every page, and I wasn't interested in keeping track of individual page hits. Some minor modifications to counter.php were needed to make this work.

First, in the area under "DO NOT EDIT BELOW", add the following code get the display mode via the "display" parameter passed in the URL:

Code: Select all

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

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

// BEGIN ADDED BLOCK
/* Get display mode
   If $display=off perform the page count, but don't echo anything to the 
   screen when using counter in quiet mode.  Modified 2011-09-03 -- JAP */
$display = input($_GET['display']);
$display = substr(trim(strtolower($display)),0,3);
// END ADDED BLOCK

/* Get page and log file names */
$page = input($_GET['page']) or die('ERROR: Missing log file \"page\" parameter');
$logfile = 'logs/' . $page . '.txt';
Then, a bit further down, modify the output statement:

Code: Select all

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

// BEGIN MODIFIED BLOCK
    /* Print out Javascript code and exit */
    /* Modified Sat. 2011-09-03 */
    if (preg_match("/off/", $display)) {
    } // do nothing
    else {
        echo 'document.write(\''.$count.'\');';
    }
    exit();
// END MODIFIED BLOCK
To use the modified script in non-displaying mode, invoke it thus in your Web page:

<script type="text/javascript" src="counter/counter.php?page=test&display=off"><!--//--></script>

You can test it by setting the $count_unique value near the top of the script to "0" (default value), then create two Web pages, one of which calls the counter with &display=off and the other the simply omits the second parameter. Refresh the page with the hidden counter a few times, then refresh the one with the displaying counter. You'll see that the counter display will have jumped by the number of times plus one that you refreshed the non-displaying page. Once you're satisfied that all is working well, set the $count_unique value to 1 and re-upload the script to the server.

The nice thing about the modified PHP script is that without the "display" parameter in the URL it works the same as it did before, so no modifications are needed to earlier applications of the counter. :wink:

Re: Running PHPcount invisibly

Posted: Wed Sep 07, 2011 11:52 am
by Klemen
Thanks for sharing!