Jump to content

Suppres visual couner display


Ralph

Recommended Posts

I am very new to PHP, so please forgive my lack of knowledge.

I downloaded a hit counter for from PHPJunkyard.com.  It works fine, but I would like to suppress the visual display and just rack the count in the log files.  The code is reproduced below:

==============================================================
<?php

# PHP hit counter (PHPcount)
# Version: 1.0
# File name: counter.php
# Written 15th July 2004 by Klemen Stirn (info@phpjunkyard.com)
# http://www.PHPJunkYard.com

##############################################################################
# COPYRIGHT NOTICE                                                          #
# Copyright 2004 PHPJunkYard All Rights Reserved.                            #
#                                                                            #
# The PHPcount may be used and modified free of charge by anyone so long as  #
# this copyright notice and the comments above 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 without prior written consent is        #
# expressly forbidden. In other words, please ask first before you try and  #
# make money off this program.                                              #
#                                                                            #
# 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 file

// Enable referer validation? 1 = YES, 0 = NO
$check_referer = 1;
// Domains that are allowed to access this script
$referers = array ("localhost","bccentre.bc.ca");

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

// Get page and log file names
$page = htmlentities($_GET['page']);
$logfile = "logs/" . $page . ".log";

// If $check_referer is set to 1 and if HTTP_REFERER is set to
// a value let's check refering site
if ($check_referer == 1 && !(empty($_SERVER['HTTP_REFERER'])))
{
check_referer($_SERVER['HTTP_REFERER']);
}

// If the log file doesn't exist we start count from 1 ...
if (! @$file = fopen($logfile,"r+"))
{
$count="1";
}
// If the log file exist lets read count from it
else {
$count = @fread($file, filesize($logfile)) or $count=0;
fclose($file);
// Raise the value of $count by 1
$count++;
}

// Write the new $count in the log file
$file = fopen($logfile,"w+") or die("Can't open/write the log file, please CHMOD logs folder to 777 (rwx-rwx-rwx)!");
fputs($file, $count);
fclose($file);

// Print out Javascript code and exit
echo "document.write('$count');";
exit();

// function that will check refering URL
function check_referer($thisurl) {
global $referers;
for ($i=0;$i<count($referers);$i++)
        {
if (preg_match("/$referers[$i]/i",$thisurl)) {return true;}
}
die("Invalid referer!");
}
?>
==============================================================
I tried remarking out echo "document.write('$count');";" tih "//" but that slowed the web site to a crawl.

Thanks for nay pointers you can give me.
Link to comment
Share on other sites

try this:

[code]<?php

# PHP hit counter (PHPcount)
# Version: 1.0
# File name: counter.php
# Written 15th July 2004 by Klemen Stirn (info@phpjunkyard.com)
# http://www.PHPJunkYard.com

##############################################################################
# COPYRIGHT NOTICE                                                          #
# Copyright 2004 PHPJunkYard All Rights Reserved.                            #
#                                                                            #
# The PHPcount may be used and modified free of charge by anyone so long as  #
# this copyright notice and the comments above 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 without prior written consent is        #
# expressly forbidden. In other words, please ask first before you try and  #
# make money off this program.                                              #
#                                                                            #
# 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 file

// Enable referer validation? 1 = YES, 0 = NO
$check_referer = 1;
// Domains that are allowed to access this script
$referers = array ("localhost","bccentre.bc.ca");

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

// function that will check refering URL
function check_referer($thisurl) {
  global $referers;
      for ($i=0;$i<count($referers);$i++)
          {
            if (preg_match("/$referers[$i]/i",$thisurl)) {return true;}
        }
  die("Invalid referer!");
}

// Get page and log file names
$page = htmlentities($_GET['page']);
$logfile = "logs/" . $page . ".log";

// If $check_referer is set to 1 and if HTTP_REFERER is set to
// a value let's check refering site
if ($check_referer == 1 && !(empty($_SERVER['HTTP_REFERER'])))
{
check_referer($_SERVER['HTTP_REFERER']);
}

// If the log file doesn't exist we start count from 1 ...
if (! @$file = fopen($logfile,"r+"))
{
$count="1";
}
// If the log file exist lets read count from it
else {
$count = @fread($file, filesize($logfile)) or $count=0;
fclose($file);
// Raise the value of $count by 1
$count++;
}

// Write the new $count in the log file
$file = fopen($logfile,"w+") or die("Can't open/write the log file, please CHMOD logs folder to 777 (rwx-rwx-rwx)!");
fputs($file, $count);
fclose($file);

// Print out Javascript code and exit
// echo "document.write('$count');";
exit();
?>[/code]

please use code tags in the future when posting code.  just makes the whole thing more readable.  i've moved the function definition up to before it gets used, and commented out the document.write statement.  not sure it'll do the trick, but give it a shot.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.