【问题标题】:Block an IP Address from adding to a website counter?阻止 IP 地址添加到网站计数器?
【发布时间】:2014-04-25 00:16:21
【问题描述】:

所以我白天多次访问我的网站以查看情况;无论是点击计数器,更新页面还是你有什么,我可能贡献了我计数器上一半以上的点击量...... 有什么办法阻止我的IP地址添加到它上面吗? http://jsfiddle.net/9ncCG/ 有随附的自述文件;这是脚本本身:

    <?php
/*******************************************************************************
*  Title: PHP hit counter (PHPcount)
*  Version: 1.4 @ March 3, 2012
*  Author: Klemen Stirn
*  Website: http://www.phpjunkyard.com
********************************************************************************
*  COPYRIGHT NOTICE
*  Copyright 2004-2012 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.
********************************************************************************
*
*  ACKNOWLEDGEMENT
*
*  Please support future script development by linking to us:
*  http://www.phpjunkyard.com/about/link2us.php
*
*  Or by sending a small donation:
*  http://www.phpjunkyard.com/about/donate.php
*
*******************************************************************************/

// SETUP YOUR COUNTER
// See file readme.html for detailed instructions

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

// 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);

/* Tell browsers not to cache the file output so we can count all hits */
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

/* Is page ID set? */
if ( ! isset($_GET['page']) )
{
    die('ERROR: The counter.php file must be called with a <b>?page=PAGEID</b> parameter, for example <b>counter.php?page=test</b>');
}

/* Remove any illegal chars from the page ID */
$page = preg_replace('/[^a-zA-Z0-9\-_\.]/','',$_GET['page']);

/* Stop if $page is not valid */
if ( ! strlen($page) )
{
    die('ERROR: Page ID is missing or contains only invalid chars. Please use only these chars for the page ID: a-z, A-Z, 0-9, &quot;.&quot;, &quot;-&quot; and &quot;_&quot;');
}

/* Set values for cookie and log file names */
$cname   = 'tcount_unique_'.$page;
$logfile = 'logs/' . $page . '.txt';

/* Does the log file exist? */
if ( ! file_exists($logfile) )
{
    die('ERROR: Log file not found. Make sure there is a file called <b>' . $page . '.txt</b> inside your <b>logs</b> folder. On most servers file names are CaSe SeNSiTiVe!');
}

/* Open log file for reading and writing */
if ($fp = @fopen($logfile, 'r+'))
{
    /* Lock log file from other scripts */
    $locked = flock($fp, LOCK_EX);

    /* Lock successful? */
    if ($locked)
    {
        /* Let's read current count */
        $count = intval( trim( fread($fp, filesize($logfile) ) ) );

        /* If counting unique hits is enabled make sure it's a unique hit */
        if ( $count_unique == 0 || ! isset($_COOKIE[$cname]) )
        {
$ips = array(
    '173.189.225.208'
);
if ( $count_unique == 0 || ! isset($_COOKIE[$cname]) )
{
    /* Update count by 1 and write the new value to the log file */
    $count = $count + 1;

    if (!in_array($_SERVER['REMOTE_ADDR'], $ips)) 
    {
        rewind($fp);
        fwrite($fp, $count);
    }

    /* Print the Cookie and P3P compact privacy policy */
    header('P3P: CP="NOI NID"');
    setcookie($cname, 1, time()+60*60*$unique_hours);
}
    }
    else
    {
        /* Lock not successful. Better to ignore than to damage the log file */
        $count = 1;
    }

    /* Release file lock and close file handle */
    flock($fp, LOCK_UN);
    fclose($fp);
}
else
{
    die("ERROR: Can't read/write to the log file ($logfile). Make sure this file is writable by PHP scripts. On UNIX servers, CHMOD the log file to 666 (rw-rw-rw-).");
}

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

/* Print out Javascript code and exit */
echo "<script>document.write(\"$count\")</script>";
exit();
?>

【问题讨论】:

    标签: php html ip counter


    【解决方案1】:

    只需在打开的评论块之后添加这个:

    $ips = array(
        '111.222.333.444',
        '111.222.333.445'
    );
    if (in_array($_SERVER['REMOTE_ADDR'], $ips)) {
        exit;
    }
    

    您要做的是将您不想计入该数组的任何 IP 地址添加到该数组中。 (显然您可以删除我作为示例添加的虚假地址)。

    更新

    试试这个:

    $ips = array(
        '111.222.333.444',
        '111.222.333.445'
    );
    if ( $count_unique == 0 || ! isset($_COOKIE[$cname]) )
    {
        /* Update count by 1 and write the new value to the log file */
        $count = $count + 1;
    
        if (!in_array($_SERVER['REMOTE_ADDR'], $ips)) 
        {
            rewind($fp);
            fwrite($fp, $count);
        }
    
        /* Print the Cookie and P3P compact privacy policy */
        header('P3P: CP="NOI NID"');
        setcookie($cname, 1, time()+60*60*$unique_hours);
    }
    

    【讨论】:

    • 非常感谢您的快速回答,但缺点是它导致命中计数器甚至不显示;我应该把它放在脚本中较低的位置吗?如果有,在哪里?
    • 对不起,还是没有计数器._.
    • "Uncaught SyntaxError: Unexpected token
    • 我对这个echo 'document.write(\''.$count.'\');';皱眉
    • 嘿,这不是我的代码哈哈。我所知道的是它有效,我可以跟随 cmets 让它做我想做的事,直到现在。
    猜你喜欢
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 2015-04-08
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多