- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我是 PHP 的新手,我正在尝试从 phpjunkyard.com 获取当前的 PHP 文本命中计数器脚本,以自动为动态页面生成计数器,而无需为每个页面创建一个 counter.txt 文件.到目前为止,如果它不存在,我设法让它为每个新页面写一个新的.txt,但我收到一个错误,我不知道如何删除它..PS:我使用这个计数器是因为它有独特的访客计数系统。
这是原始的完整 PHP 文件:
<?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 = 0;
// Number of hours a visitor is considered as "unique"
$unique_hours = 24;
// 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, ".", "-" and "_"');
}
/* 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]) )
{
/* Update count by 1 and write the new value to the log file */
$count = $count + 1;
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 'document.write(\''.$count.'\');';
exit();
?>
这就是我更改的内容(在第 82 行):
来自:
/* 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!');
}
收件人:
/* Does the log file exist? */
if ( ! file_exists($logfile) )
{
fopen($logfile, 'w') or die("can't open file");
}
因此它通过 id 为每个不同的页面创建了一个带有 .txt 的新计数器,例如:
<script language="Javascript" src="http://www.website.com/counter.php?page=<?php echo $pageid();?>">
但这是我在第一次创建 .txt 文件时使用新 ID 时遇到的错误:
Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/u716430538/public_html/counter14/counter.php on line 95
document.write('1');
之后,如果您打开相同的 ID,它将像正常情况一样显示:
document.write('1');
抱歉我的英语不好,希望有人能帮助我。谢谢
最佳答案
试试这个:
/* Does the log file exist? */
if ( ! file_exists($logfile) )
{
$f = fopen($logfile, 'w') or die("can't open file");
fwrite($f,"0");
}
关于多页面的 PHP 命中计数器自动生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12605316/
我在leetcode上看到这段代码,是一道求众数的题,下面是题目描述: 给定一个大小为 n 的数组,找到多数元素。众数元素是出现次数超过 ⌊ n/2 ⌋ 次的元素。 你可以假设数组是非空的并且多数元素
每次在 JavaScript 中执行特定操作时,例如: $(function() { $('#typing').keyup(function () { switch($(this)
我一直在为网页设计一个计数器,但我一直被这个我无法解决的功能所困扰。 我有一个 4 个 div 的计数器,因为其中两个是小数字,另外两个是大数字,所以第一个运行得很快,我看不到它们的功能。 有人知道如
我已经在文档中进行了一些搜索,并在网上花了一段时间,但找不到解决方案!我希望警报告诉我单击 .thumb 时它处于each() 的哪一次迭代。 EG:有六个.thumb,我点击数字3,浏览器弹出3!
在 Handlebars 中,假设我有 names 的集合.我能怎么做 {{#each names}} {{position}} {{name}} {{/each}} 在哪里 {{position}}
这个问题在这里已经有了答案: Numbering rows within groups in a data frame (9 个回答) 4年前关闭。 我们如何在数据帧的每组中生成唯一的 ID 号?以下
我正在努力解决以下问题。我希望为给定的“一”序列创建一个计数器。例如,我有以下内容: 1 1 1 1 0 0 1 1 1 0 0 1 1 1 1 鉴于该序列,我希望为 1 的每个序列设置一个计数器直到
我正在努力解决以下问题。我希望为给定的“一”序列创建一个计数器。例如,我有以下内容: 1 1 1 1 0 0 1 1 1 0 0 1 1 1 1 鉴于该序列,我希望为 1 的每个序列设置一个计数器直到
我有一个jsfiddle here 这是一个简单的 JavaScript 函数,可以计算出设定的数字。 是否可以进行这种计数,但也保留一位小数 所以它算 1.1、1.2、1.3 等。 func
我正在构建一个计数器,当我按下鼠标时,它应该增加到 maxValue 并且减少不超过 0。我还可以选择将计数器重置为其初始值:0。另外,如果 maxValue 是偶数,它应该计数到该数字。但是,如果
所以我成功地为字母和单词构建了其他计数器,但现在我只能用这个来计算句子。我的代码如下,当我运行它时,它会返回很多错误消息: #include #include #include int main
Closed. This question is off-topic。它当前不接受答案。
我需要一个计数器,它会随着某些任务的完成而递增。我们只需要最后一小时的值,即窗口将移动而不是静态时间。 解决此问题的最佳方法是什么?我能想到的一种方法是拥有一个大小为 60 的数组,每分钟一个,并更新
我希望使用计数器来为我提供独特的引用系统。我想单击一个按钮,然后检查一个字段/文件中的最后一个数字,然后简单地向其添加 1,然后将其插入到屏幕上的字段中? 不确定执行此操作的最佳方法或具体如何执行此操
我有一个用 php 制作的表格,在该表格内我显示了数据库中的一些内容。我在每个 td 中创建了一个简单的按钮(类似于 Like),我希望每次点击它都会增加 1。这是带有按钮的行: echo "
如何将数据库中的值转换为可用于 if else 函数的 int 值? 例如:在我的数据库“armnumber = 3”中,如何在 if else 函数中使用它? 代码 string myConnect
我需要生成唯一的“ids”,问题是,它只能在 1 - 99999 之间。 “好”的是,它仅在与另一列组合时必须是唯一的。 我们有组,每个组都有自己的“group_id”,每个组都需要类似 unique
有这个简单的代码: UPDATE counter SET c= c +1 where id = 1; 并且它在开头的 c 字段中为 null 的情况下不起作用。它只有在已经输入了一些数字时才有效,也就
我正在尝试在 python 中构建一个具有闭包属性的计数器。以下工作中的代码: def generate_counter(): CNT = [0] def add_one():
我使用 CSS 来计算 HTML 文档中的部分: body {counter-reset: sect;} section:before { counter-increment: sect;
我是一名优秀的程序员,十分优秀!