관리 메뉴

nkdk의 세상

php의 include방법과 log남기기 본문

My Programing/PHP

php의 include방법과 log남기기

nkdk 2009. 7. 24. 14:44
이번에는 php에서 include하는 방법과 log를 남기는 것에 대해서 볼까 한다.

일단 log남기는 것 부터...

get_log.php
<?php

    $log = date("Y/m/d H:i:s")."\t";
    $log .= $_SERVER["HTTP_USER_AGENT"]."\t";
    $log .= $_SERVER["HTTP_REFERER"]."\t";
    $log .= gethostbyaddr($_SERVER["REMOTE_ADDR"])."\n";
    $fh = fopen("log.txt", "a");
    flock($fh, LOCK_EX);
    fputs($fh, $log);
    flock($fh, LOCK_UN);
    fclose($fh);

?>
이걸로 일단 기본적인 파일을 만드세요. 이걸 계속 include해서 사용할 거임.

logPhp.php
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Php Etc Form Test</title>
</head>
    <body>
<?php

    include "get_log.php";

?>log save complete
<br>
<a href="display_log.php">displayLog</a>
    </body>
</html>

display_log.php
<?php

$str = <<<EOD
<table border=4 width=800 align=center
<caption>dialog log</caption>
<tr bgcolor="#888888">
    <th>time</th>
    <th>user Explorer</th>
    <th>__</th>
    <th>IP</th>
</tr>
EOD;
echo $str;

$logs = @file("log.txt") or die("log file error");
foreach($logs as $line) {
    list($time, $agent, $referer, $ip) = explode("\t", $line);
    if(!$referer) $referer="<br>";
    echo "<tr align=center><td>$time</td><td>$agent</td><td>$referer</td><td>$ip</td></tr>";
}

?>

이렇게 하고 logPhp.php를 실행해 보면 된다.

자 다음으로는 간단한 db연결 및 hashmap과 같이 쓰는법 array의 원리..  정도? 할까 생각 중