以前作成した「pc.casey.jp » [PHP] 404レポート(PHP版)」を幾つかのサイトで改良していると、やっぱり設定ファイルが別になっていないと面倒。ということで、「pc.casey.jp » [PHP] さくらインターネット リソース情報自動報告プログラム(PHP 版)」で使ったコードを流用したバージョンを作った。
主な変更点
- Ver.0.06 設定を外部ファイルに保存するようにした。これにより複数サイトに設置している場合でも本体の差し替えが簡単になった・・ハズ。
- Ver0.07 動作しているサーバーをわかりやすくするため、メール本文内にホスト名を追加した
- Ver.0.08 動作しているサーバーをわかりやすくするため、メールタイトルにホスト名を追加した
動作例
開発中のデバッグモード時の例(Ver.0.08)
実稼動の例(Ver.0.08)
スポンサードリンク
err404.php
<?php /* =================================================== * 404 Report and Suggest for mb-PHP * --------------------------------------------------- * DESCRIPTION: * - output 404 message for user * To display a polite message to visitors * * - guide for new site by searched strings * The destination guides to parse the search term. * * - site administrator get error mail * To send a message to the site administrator. * * USGE: * write .htaccess file * ErrorDocument 404 /err404.php * * !CAUTION! * Add your .htaccess file, protected conf file. * * <Files ~ "\.conf$"> * deny from all * </Files> * * LICENSE : GNU GPL * CODE : UTF-8 * LANGUAGE: PHP, ja-JP * * AUTHER : * COPYRIGHT (C) CASEY 2011 * https://pc.casey.jp/ * * VERSION : * ver.0.01 2011.09.18 first version * ver.0.02 2011.09.19 change algorithm * ver.0.03 2011.09.19 add reject function * ver.0.04 2011.09.19 change reject algorithm * ver.0.05 2011.09.29 add need referer * ver.0.06 2011.10.02 use conf file. * ver.0.07 2011.10.02 add host name to mail body. * ver.0.08 2011.10.02 add host name to subject. * =================================================== */ /* ---------------------------------- * read config file * ---------------------------------- */ require_once(dirname(__FILE__) . '/err404.conf'); /* ---------------------------------- * define [do not change!] * ---------------------------------- */ // program version $VER = "0.08"; // get start time $sTime = time(); // program name $host_name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : php_uname("n"); $program_name = "404 error report Ver."; $program_name .= $VER . " on " . $host_name; // add host name into mail subject $mail_subject .= " (" . $host_name . ")"; // dev $debug = false; // =================================================== // MAIN // =================================================== // 2011.09.19 ver.0.27 // init if($debug){ _setDebugvalue(); } // start output outputHeader(); echo getHeaderHtml(); // add ver.0.05 if(!isset($_SERVER["HTTP_REFERER"])){ if($need_referer){ $mail = false; } }else{ $keywords = getKeywords(); // return suggest if($keywords && $new_url_suggest){ getSuggest(); } } // mail send $mail_body = getMailbody(); if(!$debug && $mail){ echo "<p>この問題は管理者に通知されました。</p>"; mailSend(); } // debug info if($debug){ _debuginfo(); print_r($rejects); } // print footer echo getFooterHtml(); // end of script exit(); // =================================================== // FUNCTIONS // =================================================== // ==================================== // output footer html // ==================================== // 2011.09.19 ver.0.03 function getFooterHtml(){ $host = $_SERVER['HTTP_HOST']; return <<< EOH <div align="right"> <a id="site-link" href="http://$host" target="_blank">$host</a> </div> </body> </html> EOH; } // ==================================== // output header html // ==================================== // 2011.09.19 ver.0.03 function getHeaderHtml(){ return <<< EOH <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>404 Not Found</title> <link rel="stylesheet" href="/err.css" type="text/css" /> </head> <body id="error-page"> <h1>404 Not Found</h1> <h2>ページが見つかりません</h2> <p>リクエストされたページが見つかりませんでした。</p> EOH; } // ==================================== // mail send // ==================================== // 2011.09.19 ver.0.03 function mailSend(){ // init global $mail_addr_to, $mail_subject, $mail_body, $mail_addr_from; // set japanese mb_language("japanese"); mb_internal_encoding("UTF-8"); // mail send mb_send_mail( $mail_addr_to, $mail_subject, $mail_body, "From:" . $mail_addr_from ); } // ==================================== // debug information // ==================================== // 2011.09.19 ver.0.03 function _debuginfo(){ // init global $mail_body; echo "[ DEBUG MODE ]<br />\n"; $debug_html = $mail_body; echo preg_replace('/\n/', '<br />', $debug_html); } // ==================================== // suggest links // ==================================== // 2011.09.19 ver.0.03 function getSuggest(){ // init global $keywords, $new_url_base, $new_url_querystr; // from search engine echo "<p>もしかしたら以下に移動したかもしれません:</p>"; echo "<ul>"; $new_url = $new_url_base . $new_url_querystr; foreach($keywords as $word){ echo '<li><a href="' . $new_url . urlencode($word) .'" target="_blank">移転先(' . $new_url_base . ')を「' . $word . "」で再検索する</a></li>"; } echo "</ul>"; } // ==================================== // set debug mode value // ==================================== // 2011.09.19 ver.0.03 function _setDebugvalue(){ // add ver.0.05 global $need_referer, $mail; if(!isset($_SERVER["HTTP_REFERER"])){ $_SERVER["HTTP_REFERER"] = ''; } } // ==================================== // make mail body // ==================================== // 2011.09.19 ver.0.05 function getMailbody(){ // init global $keywords, $rejects, $program_name, $sTime, $mail; $mail_body = "ErrorID: " . $_SERVER["REQUEST_TIME"] . "\n"; //$mail_body .= "HTTP_HOST: " . $_SERVER["HTTP_HOST"] . "\n"; //$mail_body .= "REQUEST_URI: " . $_SERVER["REQUEST_URI"] . "\n"; $mail_body .= "URL: http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . " \n"; if($keywords){ if(is_array($keywords)){ $mail_body .= "QUERY:" . implode(",", $keywords) . "\n"; }else{ $mail_body .= "QUERY:" . $keywords . "\n"; } }else{ $mail_body .= "QUERY: Unknown" . "\n"; } $mail_body .= "HTTP_REFERER: " . $_SERVER["HTTP_REFERER"]. "\n"; $mail_body .= "REMOTE_ADDR: " . gethostbyaddr($_SERVER['REMOTE_ADDR']) . " (".$_SERVER['REMOTE_ADDR'].")" . "\n"; $mail_body .= "REQUEST_TIME: " . date('Y/m/d H:i:s', $_SERVER['REQUEST_TIME']) . "\n"; $mail_body .= "\n" . $program_name . " (C) https://pc.casey.jp 2011 "; $eTime = time() - $sTime; $mail_body .= " (" . $eTime . "Sec+)"; // 2011.09.19 add reject // 2011.09.19 remove // favicon.ico //$pos = strpos($_SERVER["REQUEST_URI"], 'favicon.ico'); //if($pos !== false){ $mail = false; } // 2011.09.19 add reject new if(in_array($_SERVER["REQUEST_URI"], $rejects)){ $mail = false; }; return $mail_body; } // ==================================== // start output // ==================================== // 2011.09.19 ver.0.02 function outputHeader(){ // init global $debug; if($debug){ header( 'HTTP/1.1 200 OK' ); }else{ header( 'HTTP/1.1 404 Not Found' ); } header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' ); header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' ); header( 'Cache-Control: no-cache, must-revalidate, max-age=0' ); header( 'Pragma: no-cache' ); header( 'Content-Type: text/html; charset=utf-8' ); } // ==================================== // check referer // ==================================== // 2011.09.19 ver.0.07 function getKeywords(){ // init global $new_url_extract; $keywords = null; // check ref if(isset($_SERVER["HTTP_REFERER"])){ // decode referer $_SERVER["HTTP_REFERER"] = urldecode($_SERVER["HTTP_REFERER"]); // check domain $U = @parse_url($_SERVER["HTTP_REFERER"]); // check search strings $pos = strpos($U["host"], 'google'); if($pos !== false){ //Google # q= preg_match('/q\=([^\&]+)/', $_SERVER["HTTP_REFERER"], $Qstr); }else{ //Yahoo ? p= preg_match('/p\=([^\&]+)/', $U["query"], $Qstr); } // split keywords by space to keyword array if($Qstr[1]){ // keywords extract if($new_url_extract){ $keywords = preg_split('/[\s ]+/', $Qstr[1]); }else{ $keywords[] = $Qstr[1]; } } } return $keywords; }
err404.conf
<?php /* * setting for err404.php * * !CAUTION! * Add your .htaccess file, protected conf file. * * <Files ~ "\.conf$"> * deny from all * </Files> * */ // --- suggest --- $new_url_suggest = true; $new_url_extract = true; $new_url_base = 'http://newexample.com'; $new_url_querystr = '/?s='; // --- mail ------ $mail = true; $mail_subject = "404 error report"; $mail_body = "an error occurred"; $mail_addr_from = "root@localhost"; $mail_addr_to = "[email protected]"; // --- rejects --- $need_referer = true; $rejects = array( '/robots.txt', '/favicon.ico' ); ?>
.htaccess
<Files ~ "\.conf$"> deny from all </Files>
コメント