2016-08-11 2 views
0

저는 새로운 Boston에서 "Beginner php tutorials"라는 YouTube 튜토리얼을 보면서 PHP로 코딩하는 법을 배우고 있습니다. PHP를 사용하여 고유 한 방문 횟수 카운터를 만들려고했지만 어떤 이유로 내 코드가 작동하지 않습니다. 네가 나를 도울 수 있기를 바랍니다.PHP에서 고유 한 히트 카운터 파일을 만들 때 오류가 발생했습니다.

여기 내 코드입니다 : 당신은 fuction를 서면으로 작성했습니다

<?php 

function hit_counter() { 
    $ip_address=$_SERVER['REMOTE_ADDR']; 
    echo $ip_address; 
    $ip_file=file('ip.txt'); 

    foreach ($ip_file as $ip) { 
     $ip_single= trim($ip); 
     if (@$ip_address!=$ip_single) { 
      $ip_value=false; 
      break; 
     } else { 
      $ip_value=true;  
     } 
    } 

    if (@ip_value==false) { 
     $filename='count.txt'; 
     $handle=fopen($filename, 'r'); 
     $current=fread($handle, filesize($filename)); 
     fclose($handle); 

     $count=$current+1; 

     $handle=fopen($filename, 'w'); 
     fwrite($filename, $count); 
     fclose($handle); 

     $handle=fopen('ip.txt', 'a'); 
     fwrite($ip_file, @$ip_address.'\n'); 
     fclose($handle); 
    } 
} 

?> 
+0

간단하게 할 수있는 것 같습니다 아니에요? 오류 또는 예외가 발생 했습니까? 또는 코드가 의도 한대로 코드가 수행하지 않습니까? –

답변

0

그것은 테스트를하지만, 함수가 무엇을 의미 작동하지 않습니다 다소

function hit_counter() { 
    /* sourcefile containing ips of visitors */ 
    $file=__DIR__ . '/ip.txt'; 

    /* get the user's ip address */ 
    $ip=trim($_SERVER['REMOTE_ADDR']); 

    /* read contents of sourcefile into an array */ 
    $ips=file($file); 

    /* remove blank spaces from string */ 
    array_walk($ips, function(&$v, $k){ $v=trim($v); }); 

    /* establish baseline count of unique ips */ 
    $count=count($ips); 

    /* Add new entry to file if not found & increment count */ 
    if(!in_array($ip, $ips)){ 
     file_put_contents($file, $ip . PHP_EOL, FILE_APPEND); 
     $count++; 
    } 

    /* return the count for further processing */ 
    return $count; 
} 


$hits=hit_counter(); 
echo "Hits:{$hits}"; 
+0

늦게 답변드립니다. 코드를 확인하고 작성한 새 명령을 포함합니다. :)), 감사합니다! – INME

0

하지만, 호출되지 않습니다.

function doSomething(){ 
    ... execute code here ... 
} 

// Invoke function 
doSomething(); 
관련 문제