Running PHPcount invisibly

All other scripts belong here
Post Reply
Andrew P.
Posts: 1
Joined: Mon Sep 05, 2011 7:28 pm

Running PHPcount invisibly

Post 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:
Last edited by Andrew P. on Thu Sep 08, 2011 9:16 pm, edited 1 time in total.
Klemen
Site Admin
Posts: 10116
Joined: Fri Feb 11, 2005 4:04 pm

Re: Running PHPcount invisibly

Post by Klemen »

Thanks for sharing!
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
Post Reply