Perl で、ブラウザから送られてきたデータを CGI.pm を使わずに処理する方法

最も原始的な例は以下のようになるだろう。今さらだけど、知っておいて損はないかもしれない。 HTTP プロトコルの場合、クライアントからサーバにデータを送る方法は POST メソッドか GET メソッドと仕様で定義されているから、POST の場合と GET の場合の2つのパターンを考えてデータを受け取ってあげればよい。
最も原始的な例は以下のようになるだろう。今さらだけど、知っておいて損はないかもしれない。 HTTP プロトコルの場合、クライアントからサーバにデータを送る方法は POST メソッドか GET メソッドと仕様で定義されているから、POST の場合と GET の場合の2つのパターンを考えてデータを受け取ってあげればよい。

Perl で、画像のサムネイルを作成してブラウザから表示させる方法

以下のサンプルファイルを image.cgi と名前を付けて保存、アクセス権を chmod +x image.cgi とかして ブラウザから http://yourname.com/image.cgi とかにアクセスすればよい。 注意点としては、このスクリプトは画像しか表示できないことである。これと一緒に HTML ファイルを表示しようと思っても、HTTP プロトコルの仕様上不可能である。なぜかというと HTTP プロトコルを通して画像を表示する場合は、すでにこのスクリプトが HTTP ヘッダとして Content-type: image/jpeg を最初に出力してブラウザが受け取っているからで、このスクリプトの中で同時に(画像を表示した後に) HTML 文書を表示させようとて Content-type: text/html を出力(print "Content-type: text/html\n\n)してもブラウザは受け付けてくれないのである。 #!/usr/bin/perl use GD;
以下のサンプルファイルを image.cgi と名前を付けて保存、アクセス権を chmod +x image.cgi とかして ブラウザから http://yourname.com/image.cgi とかにアクセスすればよい。 注意点としては、このスクリプトは画像しか表示できないことである。これと一緒に HTML ファイルを表示しようと思っても、HTTP プロトコルの仕様上不可能である。なぜかというと HTTP プロトコルを通して画像を表示する場合は、すでにこのスクリプトが HTTP ヘッダとして Content-type: image/jpeg を最初に出力してブラウザが受け取っているからで、このスクリプトの中で同時に(画像を表示した後に) HTML 文書を表示させようとて Content-type: text/html を出力(print "Content-type: text/html\n\n)してもブラウザは受け付けてくれないのである。 #!/usr/bin/perl use GD;

Requirements for Mobile Phones (English)

I would like to propose the unique requirements for mobile phones. The following items are significant considerations under mobile site development, especially in i-mode phones.
  1. UserAgent type that is, a Browser Name of CGI environments which is sent by a mobile phone to a web server.
  2. Range of IP addresses in each mobile operator, which is required to judge whether a user is using a mobile phone or a PC in order to support both of them in the web site. In Japanese mobile phone operators, they are using their specific proxy servers, which are accompanying with the specific range of IP addresses.
  3. Screen Size of each mobile phone, especially the size of width, which would be found in a CGI environment UserAgent in the latest phones
  4. Image Format such as JPEG, PNG and GIF supported by each mobile phone
  5. Maximum Size of Receiving Bytes per a page
  6. Difference of Handling Colors in each mobile phone
  7. Difference of Smilies (Picture Icons) both in each mobile phone and operator
  8. Name Convention of an E-mail Address in each operator (Japanese i-mode e-mail system violates RFC).
  9. Available Character Set in each operator (Shift-JIS, UTF-8 in Japan)
  10. Available Character Types in each operator (such as ZENKAKU, HANKAKU in Japan)
  11. Available Input Method in each operator (Access-key concept in CHTML)
  12. Support of Native Functions of a mobile phone (i.e. switching a browser screen to an e-mail application with 'mailto:' in CHTML at a single screen of a mobile phone, launching Java application, and directly calling to someone with 'tel:' in CHTML, etc)
  13. Difference in between Official Sites and Unofficial Sites (The acquiring information is different)
  14. Session Management because we cannot use cookie in i-mode phones
I would like to propose the unique requirements for mobile phones. The following items are significant considerations under mobile site development, especially in i-mode phones.
  1. UserAgent type that is, a Browser Name of CGI environments which is sent by a mobile phone to a web server.
  2. Range of IP addresses in each mobile operator, which is required to judge whether a user is using a mobile phone or a PC in order to support both of them in the web site. In Japanese mobile phone operators, they are using their specific proxy servers, which are accompanying with the specific range of IP addresses.
  3. Screen Size of each mobile phone, especially the size of width, which would be found in a CGI environment UserAgent in the latest phones
  4. Image Format such as JPEG, PNG and GIF supported by each mobile phone
  5. Maximum Size of Receiving Bytes per a page
  6. Difference of Handling Colors in each mobile phone
  7. Difference of Smilies (Picture Icons) both in each mobile phone and operator
  8. Name Convention of an E-mail Address in each operator (Japanese i-mode e-mail system violates RFC).
  9. Available Character Set in each operator (Shift-JIS, UTF-8 in Japan)
  10. Available Character Types in each operator (such as ZENKAKU, HANKAKU in Japan)
  11. Available Input Method in each operator (Access-key concept in CHTML)
  12. Support of Native Functions of a mobile phone (i.e. switching a browser screen to an e-mail application with 'mailto:' in CHTML at a single screen of a mobile phone, launching Java application, and directly calling to someone with 'tel:' in CHTML, etc)
  13. Difference in between Official Sites and Unofficial Sites (The acquiring information is different)
  14. Session Management because we cannot use cookie in i-mode phones

Perl で、JavaScript と連動してサーバにアクセスログを残す方法

ウェブページへのアクセスログを記録する方法は、いろいろな方法があると思う。もっとも簡単なのは Apache などのウェブサーバが自動的に記録するログをチェックすることで、これには何の手間もいらない。しかし、レンタルサーバなどでログが自由に扱えない場合はどうしたらいいのだろう? そのひとつが、画像としてリンクされている Perl などの CGI を呼び出す方法だ。利用者に知られずこっそりとアクセスログを取るには 1x1 ピクセルの画像をそのウェブページの背景と共に表示してしまえば、そのウェブページのソースをチェックされない限り、ほとんどの場合は気づかれずにすむだろう。 そんなわけで、以下のように JavaScript と対応する Perl スクリプトで、アクセスログを記録する仕組みを作ってみた。ログの形式は Apache に準拠する(たぶん)。
ウェブページへのアクセスログを記録する方法は、いろいろな方法があると思う。もっとも簡単なのは Apache などのウェブサーバが自動的に記録するログをチェックすることで、これには何の手間もいらない。しかし、レンタルサーバなどでログが自由に扱えない場合はどうしたらいいのだろう? そのひとつが、画像としてリンクされている Perl などの CGI を呼び出す方法だ。利用者に知られずこっそりとアクセスログを取るには 1x1 ピクセルの画像をそのウェブページの背景と共に表示してしまえば、そのウェブページのソースをチェックされない限り、ほとんどの場合は気づかれずにすむだろう。 そんなわけで、以下のように JavaScript と対応する Perl スクリプトで、アクセスログを記録する仕組みを作ってみた。ログの形式は Apache に準拠する(たぶん)。

PHP で、連続カウント防止・カンマ区切り付きカウンタを作成する

PHP で、以下の仕様を満たすカウンタを作成してみる (このサイトで使っています)。
  1. 同一 IP からの連続したアクセスをカウントしない (連続カウント防止)。
  2. カウンタの数字をカンマ (,) で区切って表示する。
連続カウント防止機能は、
if(($ip_check == 1 && $ip != $_SERVER['REMOTE_ADDR']) ...
の部分でチェックしている。$ip には、「直前にアクセスしてきた IP アドレス」が入っている。同じ IP アドレスからアクセスし続ける限りはカウントされない。通常はこれでわりと正確なカウンタとなり得ると思うが、ファイアウォールの内側にいる同一マシンなどで、ファイアウォールのアドレスがアクセスのたびに変わる場合はこの限りではない。また、ケータイからのアクセスなどの場合も毎回アクセス元の IP アドレスがある一定の範囲内で変わるので連続カウント防止にはならないことに注意。 カウンタの数字をカンマ区切りにしているのは以下の 1行である。
$total = number_format($total);      // カンマ区切りにする
以下は PHP の全ソース。
<?php

// count.php
// (c) 2006 twinkle.cc
// <a />
//" title="http://perltips.twinkle.cc/
//">http://perltips.twinkle.cc/
//</a> updated by yas 2006/01/03
// updated by yas 2003/05/07
// updated by yas 2003/03/05
// オリジナル: <a />
//" title="http://works.xrea.jp/
//">http://works.xrea.jp/
//</a> ※このスクリプトと同じディレクトリに「count.dat」という名前のファイルを作成し、置いておくこと。

if(!$file) { $file='count.dat'; }

// 設定 ここから ────────────────────────────

$HOME = 'count.datがあるディレクトリのパス'; // ディレクトリ
$data = "$HOME/$file";               // カウント記録ファイル
$ip_check = 1;                       // 連続カウント防止(yes=1 no=0)

// 設定 ここまで ────────────────────────────

$count = file($data);                            // ファイルを配列に

// データ内の文字列を分解してそれぞれの変数に代入
list($total, $ip)=explode('<>', $count[0]);

// カウントアップ処理
if(($ip_check == 1 && $ip != $_SERVER['REMOTE_ADDR'])
||   
$ip_check == 0) {
   
$total++;
   
$new_data = implode('<>', array($total, $_SERVER['REMOTE_ADDR']));
   
$fp = fopen($data, 'w');         // 書きモードでオープン
   
flock($fp, LOCK_EX);             // ファイルロック
   
fputs($fp, $new_data);           // 書き込み
   
flock($fp, LOCK_UN);             // ロック解除
   
fclose($fp);                     // ファイルを閉じる
}
$total = number_format($total);      // カンマ区切りにする
print $total;                        // カウンタを表示
?>
このソースを自身の PHP のカウンタを表示したいページを同じディレクトリに置き、同じディレクトリに count.dat という名前のファイルを置く (私のところは「chmod 644 count.dat」で動いているがお使いの環境によってはパーミッションに注意)。 最後に、カウンタを表示したい場所に以下のように書けばよい。
<?php
include('count.php');
?>
PHP で、以下の仕様を満たすカウンタを作成してみる (このサイトで使っています)。
  1. 同一 IP からの連続したアクセスをカウントしない (連続カウント防止)。
  2. カウンタの数字をカンマ (,) で区切って表示する。
連続カウント防止機能は、
if(($ip_check == 1 && $ip != $_SERVER['REMOTE_ADDR']) ...
の部分でチェックしている。$ip には、「直前にアクセスしてきた IP アドレス」が入っている。同じ IP アドレスからアクセスし続ける限りはカウントされない。通常はこれでわりと正確なカウンタとなり得ると思うが、ファイアウォールの内側にいる同一マシンなどで、ファイアウォールのアドレスがアクセスのたびに変わる場合はこの限りではない。また、ケータイからのアクセスなどの場合も毎回アクセス元の IP アドレスがある一定の範囲内で変わるので連続カウント防止にはならないことに注意。 カウンタの数字をカンマ区切りにしているのは以下の 1行である。
$total = number_format($total);      // カンマ区切りにする
以下は PHP の全ソース。
<?php

// count.php
// (c) 2006 twinkle.cc
// <a />
//" title="http://perltips.twinkle.cc/
//">http://perltips.twinkle.cc/
//</a> updated by yas 2006/01/03
// updated by yas 2003/05/07
// updated by yas 2003/03/05
// オリジナル: <a />
//" title="http://works.xrea.jp/
//">http://works.xrea.jp/
//</a> ※このスクリプトと同じディレクトリに「count.dat」という名前のファイルを作成し、置いておくこと。

if(!$file) { $file='count.dat'; }

// 設定 ここから ────────────────────────────

$HOME = 'count.datがあるディレクトリのパス'; // ディレクトリ
$data = "$HOME/$file";               // カウント記録ファイル
$ip_check = 1;                       // 連続カウント防止(yes=1 no=0)

// 設定 ここまで ────────────────────────────

$count = file($data);                            // ファイルを配列に

// データ内の文字列を分解してそれぞれの変数に代入
list($total, $ip)=explode('<>', $count[0]);

// カウントアップ処理
if(($ip_check == 1 && $ip != $_SERVER['REMOTE_ADDR'])
||   
$ip_check == 0) {
   
$total++;
   
$new_data = implode('<>', array($total, $_SERVER['REMOTE_ADDR']));
   
$fp = fopen($data, 'w');         // 書きモードでオープン
   
flock($fp, LOCK_EX);             // ファイルロック
   
fputs($fp, $new_data);           // 書き込み
   
flock($fp, LOCK_UN);             // ロック解除
   
fclose($fp);                     // ファイルを閉じる
}
$total = number_format($total);      // カンマ区切りにする
print $total;                        // カウンタを表示
?>
このソースを自身の PHP のカウンタを表示したいページを同じディレクトリに置き、同じディレクトリに count.dat という名前のファイルを置く (私のところは「chmod 644 count.dat」で動いているがお使いの環境によってはパーミッションに注意)。 最後に、カウンタを表示したい場所に以下のように書けばよい。
<?php
include('count.php');
?>