最終更新日 2004/9/10  戻る  493731 人 REMOTE = 38.103.63.59 REFERER =

★mod_layoutを使ってみる

全てのページにfooterやheaderを使いたいことがあります.共通の部分を別ファイルにしてSSIなどで表示することもできますが,apacheではmod_layoutというmoduleがあります.http://software.tangent.org/ からDownloadできます.apache2用は現在mod_layout 4.0.2aが最新です.

1.インストール
いつものように/usr/local/srcにmod_layout-4.0.2a.tar.gzがあるとします.
#cd /usr/local/src
#tar zxvf mod_layout-4.0.2a.tar.gz
#cd mod_layout-4.0.2a
Makefileの編集
APXS=/usr/local/apache2/bin/apxs
APACHECTL=/usr/local/apache2/bin/apachectl
と変更.distributionによってこの部分は違ってきます.
#make
#make install
これによって/usr/local/apache2/modulesにmod_layout.soがコピーされ,httpd.confに
LoadModule layout_module modules/mod_layout.so
が追加されます.
あとは,httpd.confでも.htaccessでもよいですが
AddOutputFilter LAYOUT html
LayoutFooter /footer.html
LayoutHeader /header.cgi
を追加します.(httpd.confでは<Directory /home/*/httpdocs>もしくは<VirtualHost *>に追加する)
.htaccessのほうがディレクトリー毎に制御できますから都合がいいです.
footer.htmlの部分は*.plでも*.cgiでも<hr noshade>のようなhtml構文でも何でも良いです.

2.問題点
本文のhtmlの中にSSIが使ってあると不都合が出ます.(SSIが上手く表示できない.本文が2重に表示される.)しかし,footer.htmlやheader.htmlの中では問題なく動きます.ですからSSIが使いたいときは本文の中ではSSIを使わずfooterやheaderの中でSSIを使います.もしかすると何かのオプションでこの不都合は回避できるかも知れません.現時点では分からないのでもし知っている人がいたらメール下さい.これはapache2専用の4.0.2aのためかも知れません.apache1.3系の3.2.1ではそのような不具合は出ないかもしれませんが試してません.また,3.2.1はapache2で使えるのか?これも試してない.そのうち試しましょう.でも鷹巣のhttp://sakaguch.com/PastBBS/0014/B0008772.html
をみると3.2.1はapache2系ではだめっぽい.

3.実際の使い方
LayoutHeaderやLayoutFooterにhtmlを指定した場合<html>や<body>は要りません.ただし,*.cgiや*.plを指定したときには
Content-type: text/html
(return)
が必要となります.下記を参照.
a.DocumentRootの.htaccess
Options ExecCGI Includes
AddHandler server-parsed .shtml
AddHandler server-parsed .html
AddHandler server-parsed .htm
AddType text/css .css
AddType application/x-httpd-cgi .cgi
AddType application/x-httpd-cgi .pl
AddType application/x-httpd-php .php
DirectoryIndex index.html index.htm index.php
AddOutputFilter LAYOUT html
LayoutHeader /header.html
LayoutFooter /footer.html


/header.html
<P align="center"><IMG src="image1.gif" width="675" height="65" border="0"></P>
<P align="center"><FONT face="DFPOP体">ここは</FONT><FONT face="Impact"><I>Yaguma's HP</I></FONT><FONT face="DFPOP体">です.下記のようなコンテンツからなっています<BR>
</FONT></P>
<P align="center"><!--#include virtual="./count.cgi" -->人  <!--#config timefmt="%Y/%m/%d" --><!--#flastmod file="index.html" --> 更新<BR>
Date : <!--#config timefmt="%Y/%m/%d %X" --><!--#echo var="DATE_LOCAL" --><BR>
Your host machine's IP is <!--#echo var="REMOTE_ADDR" --><BR>
Your web browser is <!--#echo var="HTTP_USER_AGENT" --><BR>

/footer.html
<hr noshade>
<P align="center"><IMG src="../yaguma-banner.gif" width="88" height="31" border="0" align="middle"> Copyright 2002-2003 Kai All Rights Reserved &amp; This site is Link Free</P>

b./Serverの.htaccess
AddOutputFilter LAYOUT html
LayoutHeader /Server/header.cgi
LayoutFooter /Server/footer.html

/Server/header.cgi
#!/usr/bin/perl
$time = (stat $ENV{'LAYOUT_FILENAME'})[9];
($sec,$min,$hour,$day,$mon,$year)=localtime($time);
$year+=1900;
$mon++;
print <<"EOL";
Content-type: text/html

最終更新日 $year/$mon/$day
<A HREF="#" onClick="history.back()"> 戻る</A>
<hr noshade>
EOL

/Server/footer.html
<hr noshade>
<P align="center"><IMG src="../yaguma-banner.gif" width="88" height="31" border="0" align="middle"> Copyright 2002-2003 Kai All Rights Reserved &amp; This site is Link Free<A HREF="#" onClick="history.back()"> 戻る</A></P>

c. phpにmod_layoutを適用したい場合は.htaccessを
AddOutputFilter LAYOUT php
LayoutHeader /Server/header.cgi
LayoutFooter /Server/footer.html
などとします.

4.Perlでの使い方
mod_layoutでは
LAYOUT_FILENAME /home/user/httpdocs/index.html(DocumentRoot)
LAYOUT_PATH_INFO
LAYOUT_QUERY_STRING
LAYOUT_SCRIPT_NAME /index.html
の4つが環境変数に加わります.上記の/Server/header.cgiのようにLAYOUT_FILENAMEを用いて更新日などを表示することが可能です.
/Server/header.cgiにカウンタを付けてみました.

#!/usr/bin/perl
$file = $ENV{'LAYOUT_FILENAME'};
$time = (stat $file)[9];
($sec,$min,$hour,$day,$mon,$year)=localtime($time);
$year+=1900;
$mon++;

print("Content-type: text/html\n\n");
print("最終更新日 $year/$mon/$day\n ");
if($file eq '/home/user/httpdocs/Server/oyaji.html'){
        print('<A href="../index.html">戻る</A>');
}elsif($file eq '/home/user/httpdocs/Server/server.html'){
        print('<A href="../index.html">戻る</A>');
}elsif($file eq '/home/user/httpdocs/ramen/ramen.html'){
        print('<A href="../index.html">戻る</A>');
}else{ print('<A href="./server.html">戻る</A>')};

$logfile = "../count.log";              #カウンタ保存用ファイル
$aclogfile = "../access.log";           #アクセス記録用ファイル
$addr = $ENV{'REMOTE_ADDR'};
$referer = $ENV{'HTTP_REFERER'};
$url       = "yaguma.com";
$keep      = 10;                        #アクセス記録の保存期間(10秒)
$now       = time;

open(FILE, "+<$aclogfile");
flock(FILE,2);
@logs = <FILE>;
@logs = grep($_ > $now - $keep, @logs);
seek(FILE,0,0);
print FILE "$now\t$addr\n";
print FILE @logs;
truncate(FILE, tell);
flock(FILE,8);
close(FILE);
open(FILE,"+<$logfile");
flock(FILE,2);
chop($count = <FILE>);

if ($referer !~ /$url/) {               #外部からのアクセスであり
  if (&is_newcomer()) {                 #新規訪問者によるアクセスなら
    $count++;                           #カウンタを+1する
    seek(FILE,0,0);
    print FILE "$count\n";
  }
}
flock(FILE,8);
close(FILE);
printf("  "."%05d"." 人", $count);
print " REMOTE = ".$addr;
print " REFERER = ".$referer;
print("<hr noshade>");

sub is_newcomer {                       #重複アクセスチェック
  foreach $line (@logs) {
    chop($line);
    ($t, $a) = split(/\t/, $line);
    if ($addr eq $a) {
      return 0;
    }
  }
  return 1;
}

Copyright 2002-2003 Kai All Rights Reserved & This site is Link Free 戻る