2012-09-26 1 views
2

안녕하세요, 저는 PHP가있는 초보자입니다. phpjunkyard.com에서 카운터 스크립트를 생성하여 현재 생성하지 않고도 동적 페이지 카운터를 자동으로 생성하려고합니다. 모든 페이지에 대한 counter.txt 파일 지금까지 나는 모든 새로운 페이지를위한 새로운 .txt를 작성하기 위해 그것이 존재하지 않는다면 얻으려고하지만 오류가 발생하고 그것을 제거하는 방법을 모른다. .. 추신 : 나는이 카운터를 사용하고있다. 방문자 수 계산 시스템이 독특하기 때문입니다.PHP 멀티 카운터에 대한 자동 카운터 생성

이 원래의 전체 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, &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])) 
     { 
      /* 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"); 
} 

그래서 새 coun를 만들었습니다. 같은 ID로 모든 다른 페이지에 대한이 .txt TER :

:

<script language="Javascript" src="http://www.website.com/counter.php?page=<?php echo $pageid();?>"> 

는하지만이 단지는 .txt 파일을 만드는 것 처음에 새로운 아이디에 내가지고있어 오류입니다 내 나쁜 영어

document.write('1'); 

죄송합니다 누군가가 나를 도울 수 있기를 바랍니다 : 다음 시간 이후에

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가 정상처럼 보여 것이라는 점을 다시 열 경우. 감사

답변

2

이 시도 :

그것은 작동
/* Does the log file exist? */ 
if (! file_exists($logfile)) 
{ 
$f = fopen($logfile, 'w') or die("can't open file"); 
fwrite($f,"0"); 
} 
+0

, 감사합니다! (: –