2013-09-07 6 views
0

이 PHP 코드를 사용하여 내 사이트 방문자 수를 조회합니다 (방문 횟수 카운터). 현재이 번호가 단일 블록에 표시되도록 설정되어 있습니다. 즉, 각 숫자의 스타일을 다르게 지정하려면 각 숫자를 고유 한 HTML 태그에 표시하고 싶습니다.php 메소드로 방문 횟수 카운터를 구분하고 스타일을 지정하려면 어떻게해야합니까?

저는 $ allHits var에서 explode 및 substr을 실험했지만 좋은 결과를 얻지 못했습니다. 어떤 아이디어?

<? 

/*---------------------------- 

-------- ++ simPHP ++ -------- 
A simple PHP hit counter. 

Description: 
    simPHP counts both regular and unique views on multiple 
    webpages. The stats can be displayed on any PHP-enabled 
    webpage. You MUST have read/write permissions on files. 

Script by Ajay: [email protected] 
http://scyberia.org 

----------------------------*/ 



/*----------CONFIG----------*/ 

// NOTE: If you change any config after using simphp, 
// remove the old files. 

// Relative URL of text file that holds hit info: 
$lf_name = "hits.txt"; 

// Save new log file each month 
// 0 = No 
// 1 = Yes 
$monthly = 1; 

// Path to store old files: 
// Default for June, 2012: 
// oldfiles/6-12.txt 
$monthly_path = "oldfiles"; 

// Count unique hits or all hits: 
// 0 = All hits 
// 1 = Unique hits 
// 2 = Both 
$type = 2; 

// Text to display 
// before all hits. 
$beforeAllText = "Site Visitors: "; 

// Before unique hits. 
$beforeUniqueText = "Unique Visits: "; 

// Display hits on this page: 
// 0 = No 
// 1 = Yes 
$display = 1; 

// Only change this if you are recording both values. 
// Separator for unique and all hits display - use HTML tags! (line break is default) 
$separator = "<br \>"; 

// Default would output: 
// Visits: 10 
// Unique Visits: 10 
/*--------------------------*/ 






/*--------BEGIN CODE--------*/ 

$log_file = dirname(__FILE__) . '/' . $lf_name; 

//Check for "?display=true" in URL. 
if ($_GET['display'] == "true") { 
    //Show include() info. 
    die("<pre>&#60;? include(\"" . dirname(__FILE__) . '/' . basename(__FILE__) . "\"); ?&#62;</pre>"); 
} else { 
    //Visit or IP. 
    $uIP = $_SERVER['REMOTE_ADDR']; 

    //Check for "hits.txt" file. 
    if (file_exists($log_file)) { 
     //Check if today is first day of month 
     if (date('j') == 10) { 
      //Ensure that monthly dir exists 
      if (!file_exists($monthly_path)) { 
       mkdir($monthly_path); 
      } 

      //Check if prev month log file exists already 
      $prev_name = $monthly_path . '/' . date("n-Y", strtotime("-1 month")); 
      if (!file_exists($prev_name)) { 
       //If not, move/rename current file 
       copy($log_file, $prev_name); 

       //Create new $toWrite based on CONFIG 
       //Write file according to CONFIG above. 
       if ($type == 0) { 
        $toWrite = "1"; 
        $info = $beforeAllText . "1"; 
       } else if ($type == 1) { 
        $toWrite = "1;" . $uIP . ","; 
        $info = $beforeUniqueText . "1"; 
       } else if ($type == 2) { 
        $toWrite = "1;1;" . $uIP . ","; 
        $info = $beforeAllText . "1" . $separator . $beforeUniqueText . "1"; 
       } 
       goto write_logfile; 
      } 
     } 

     //Get contents of "hits.txt" file. 
     $log = file_get_contents($log_file); 

     //Get type from CONFIG above. 
     if ($type == 0) { 

      //Create info to write to log file and info to show. 
      $toWrite = intval($log) + 1; 
      $info = $beforeAllText . $toWrite; 

     } else if ($type == 1) { 

      //Separate log file into hits and IPs. 
      $hits = reset(explode(";", $log)); 
      $IPs = end(explode(";", $log)); 
      $IPArray = explode(",", $IPs); 

      //Check for visitor IP in list of IPs. 
      if (array_search($uIP, $IPArray, true) === false) { 
       //If doesnt' exist increase hits and include IP. 
       $hits = intval($hits) + 1; 
       $toWrite = $hits . ";" . $IPs . $uIP . ","; 
      } else { 
       //Otherwise nothing. 
       $toWrite = $log; 
      } 
      //Info to show. 
      $info = $beforeUniqueText . $hits; 

     } else if ($type == 2) { 

      //Position of separators. 
      $c1Pos = strpos($log, ";"); 
      $c2Pos = strrpos($log, ";"); 

      //Separate log file into regular hits, unique hits, and IPs. 
      $pieces = explode(";", $log); 
      $allHits = $pieces[0]; 
      $uniqueHits = $pieces[1]; 
      $IPs = $pieces[2]; 
      $IPArray = explode(",", $IPs); 

      //Increase regular hits. 
      $allHits = intval($allHits) + 1; 

      //Search for visitor IP in list of IPs. 
      if (array_search($uIP, $IPArray, true) === false) { 
       //Increase ONLY unique hits and append IP. 
       $uniqueHits = intval($uniqueHits) + 1; 
       $toWrite = $allHits . ";" . $uniqueHits . ";" . $IPs . $uIP . ","; 
      } else { 
       //Else just include regular hits. 
       $toWrite = $allHits . ";" . $uniqueHits . ";" . $IPs; 
      } 
      //Info to show. 
      $info = $beforeAllText . $allHits . $separator . $beforeUniqueText . $uniqueHits; 
     } 
    } else { 
     //If "hits.txt" doesn't exist, create it. 
     $fp = fopen($log_file ,"w"); 
     fclose($fp); 

     //Write file according to CONFIG above. 
     if ($type == 0) { 
      $toWrite = "1"; 
      $info = $beforeAllText . "1"; 
     } else if ($type == 1) { 
      $toWrite = "1;" . $uIP . ","; 
      $info = $beforeUniqueText . "1"; 
     } else if ($type == 2) { 
      $toWrite = "1;1;" . $uIP . ","; 
      $info = $beforeAllText . "1" . $separator . $beforeUniqueText . "1"; 
     } 
    } 

    write_logfile: 
    //Put $toWrite in log file 
    file_put_contents($log_file, $toWrite); 

    //Display info if is set in CONFIG. 
    if ($display == 1) { 

    } 
} 

이처럼 foreach 루프 작업을 사용합니다 :

$array_allHits = explode(" ",$allHits); 
foreach ($array_allHits as $display_digits) { 
     echo $display_digits[0]; 

} 

답변

0

당신은에 대한 루프를 사용할 수 있습니다.

for ($i = 0; $i < strlen((string) $allHits); $i++) { 
    echo '<span>' . $allHits[$i] . '</span>'; 
} 

위의 코드는 적중 횟수의 모든 숫자에 대해 <span>{number}</span>을 출력합니다. 배열 인덱스가 처리되는 것과 유사하게 문자열의 모든 문자를 반복합니다. 당신이 아래 CSS에서

+0

죄송합니다 (문자열) 특정 뭔가 할 생각입니까? –

0
$totalHits = 125655; 
foreach(str_split((string)$totalHits) as $key => $val) { 
    echo "<span class='position_{$key}'>{$val}</span>"; 
} 

스타일을 추가

.position_0 { 
    color: red; 
} 
.position_1 { 
    color: blue; 
} 
.position_2 { 
    color: yellow; 
}