php를 사용한 홈페이지 접속 로그 파일

gang의 이미지

졸업한지가 한참이 지났지만, 아직 재학 시절의 연구실 서버에 제홈페이지를 운영하고 있습니다. 그러다보니 서버에 대한 제어를 맘대로 할 수 없어서, 입맛대로 맘껏 홈페이지를 꾸밀 수는 없는 처지죠. 하지만, 좀 예전 버젼의 php는 사용할 수가 있더군요.
아래 코드는 제 홈페이지에 누가 다녀가는지 볼려구 만들어본 코드입니다. 며칠 돌려보니, 흑흑..., 구글이나 msn 따위의 검색 로봇들만 다녀가는군요.

acc_log.php

<?
  define("LOG_FILE", "log file 경로");

  $ref = $HTTP_SERVER_VARS["HTTP_REFERER"];
  $addr = $HTTP_SERVER_VARS["REMOTE_ADDR"];
  $agent = $HTTP_SERVER_VARS["HTTP_USER_AGENT"];

  function access_log($path)
  {
        $addlist="log를 남길 필요가 없는 IP 주소, 즉 나 자신이 사용하는 PC의 IP 따위";
        global $ref, $addr, $agent;

        if(strstr($addlist, $addr)) return;

        $weekday = array("일","월","화","수","목","금","토");
        $tarr = localtime();
        
        $logstr = ($tarr[5]+1900)."/".($tarr[4]+1)."/$tarr[3] ".
                $weekday[$tarr[6]].
                " $tarr[2]:$tarr[1]:$tarr[0]\t".
                "$path \t$addr \t$ref \t$agent\n";

        $fp = fopen(LOG_FILE, 'a');
        fputs($fp, $logstr);
        fclose($fp);

        return;
  }
?>

이때, 로그 파일의 access 권한은 rw-r--rw- 로 write 가능하게 해야 합니다.

그리고, index.php 같은 페이지에 다음 코드를...

<?php
  include("acc_log.php");
  access_log("/");
?>
Forums: 
kirrie의 이미지

위 로그 발생기로 생성된 로그파일을 기반으로 웹상에서 로그를 볼 수 있도록 로그 뷰어를 만들어봤습니다.
view_log.php파일은 acc_log.php파일이 존재하는 같은 디렉토리에 있으면 됩니다.

view_log.php

<html>
<head>
<title>Log Viewer</title>
<style>
.title {
	background-color : #F4F4F4;
	font-weight : bold;
	font-size : 12px;
	text-align : center;
}
.content {
	background-color : #FFFFFF;
	font-size : 12px;
}
a {
	font-size : 12px;
}
a:hover {
	font-size : 12px;
}
</style>
</head>
<body>
	<h2>Log Viewer</h2>
	<table width="800" cellpadding="1" cellspacing="1" style="background-color : #CCCCCC;">
		<tr height="30">
			<td class="title">Date</td>
			<td class="title">Path</td>
			<td class="title">IP</td>
			<td class="title">Referer</td>
			<td class="title">WebBrowser</td>
		</tr>
<?
	include "./acc_log.php";

	$rowPerPage = 10; //페이지당 보여질 로그 수
	
	$page = page($_GET['page']);

	$fp = fopen(LOG_FILE, 'r');
	$buffer = fread($fp, filesize(LOG_FILE));
	$row = explode("\n", $buffer);
	rsort($row);
	for($i = $page['start']; $i < $page['end']; $i++) {
		$col = explode("\t", $row[$i]);
		if($col[0] != "") {
?>
		<tr height="25">
			<td class="content"><?=$col[0];?></td>
			<td class="content"><?=$col[1];?></td>
			<td class="content"><?=$col[2];?></td>
			<td class="content"><?=$col[3];?></td>
			<td class="content"><?=$col[4];?></td>
		</tr>
<?
		}
	}
	fclose($fp);
?>
	</table>
	<? print_page(sizeof($row), $_GET['page']);?><br>
	Total Page View : <?=(sizeof($row)-1);?>
</body>
</html>
<?
	//페이지 정리
	function page($int) {
		global $rowPerPage;

		if(!isset($int)) $int = 1;

		$page['end'] = ($rowPerPage*$int);
		$page['start'] = ($page['end'] - $rowPerPage);

		return $page;
	}

	//페이지 출력
	function print_page($row, $page) {
		global $rowPerPage;

		$row = ($row / $rowPerPage);
		if(!isset($page)) $page = 1;

		if(ceil($row) != 0) {
			for($i = 1; $i <= ceil($row); $i++) {
				if($page == $i) {
					echo "<span style=\"font-weight : bold; color : #000000;\">[".$i."]</span>";
				} else {
					echo "<a href=\"./view_log.php?page=".$i."\">[".$i."]</a>";
				}
			}
		} else {
			echo "No Page";
		}
	}
?>

페이징 기능도 넣어 봤는데, 로그가 기하급수로 늘어날 경우 페이지 링크도 기하급수로 늘어날 것 같습니다. 일단 이 부분은 패스! 다른 초고수님께서 해결해 주실 것으로 믿사옵니다.

--->
데비안 & 우분투로 대동단결!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.