作成
BOTのアクセスだけ排除する!GooglePlaceAPI 利用者のために~
google place API を使って 緯度経度であれこれしてるのですが。大量のボットのアクセスのせいで、#R+#数日で数万円課金されてorz ###center (img://18/247m) ### いちおう 料金確認はこちら。(https://console.cloud.google.com/billing/) おそらく数時間に一回請求が更新されるようですが、 #R+#見るたびにしかも深夜帯に数千円単位で増えて・・・ ので 破産しないために徹夜で処置。 ###memo ret 推測で、#R+#犯人はたぶんロボットだろう・・・ と察しがついてたので、あたりをつけて。ちなみに環境は CentOS7で、PHP5 の いわゆるLAMP です。 ###command (ログの確認) // SSLの場合 tail -f /var/log/httpd/ssl_access_log // SSLじゃない場合 tail -f /var/log/httpd/access_log ###command (アクセスの多かったUA の文字列の一部分) grapeshotcrawler、googlebot、bingbot、semrushbot、mj12bot ###memo robots.txt やなんやでやろうかとおもったけど、よくわからないクローラーなんかもあるので、プログラムで対応。 ###title いちおうプログラム抜粋 ### UAを確認すると、bot,crawl などの文字列でパターンマッチングして排除がてっとりばやそうです。 ###title3 UAで排除 ###source (php) $ua = trim($_SERVER["HTTP_USER_AGENT"]); $ua = mb_strtolower($ua) ; //半角に if ( preg_match("/bot|crawl|daum/", $ua ) || $ua=='' ) { // とりあえず bot.html に飛ばす。 header("Location: /bot.html") ; } ###title3 IPアドレスで排除 ###memo じつは、結局使わなかったのですが・・・ ###source // 排除したいIP $ip_black_list[] = "xxx.xxx.xxx.xxx" ; $ip_black_list[] = "yyy.yyy.yyy.yyy" ; // アクセスしてきたIP $ip = $_SERVER["REMOTE_ADDR"] ; if (in_array($ip, $ip_black_list)) { header("Location: /bot.html") ; } ###memo #*#in_array関数 が便利。同じように正規表現を使う場合は #*#preg_grep関数 ###title3 ###title3 おまけ)ログ書き出し。 ###memo アクセスログを書き出すと、大量になるので。 bingやgoogle などの UAをもつやつはログにさえ書き出さない。みたいなプログラム。 ###source $log = "/tmp/test.log" $write = true ; // こいつらはログにかかない。 if ( preg_match("/bingbot|semrushbot|dotbot|google/", $ua) ) $write=false ; if ( $write) file_put_contents($log , "SUCCESS¥t" . $ua . "¥t" . $ip . "¥n", FILE_APPEND | LOCK_EX); ###title 予算とアラート ###memo ちなみに APIを使用して、設定予算オーバーするとアラートがくる仕組みは googleさんにあります。ただ、請求が止まるわけではないです。 ###explain きてたメール ###center (img://18/250m) ###explain 予算設定(https://console.cloud.google.com/billing/) ###center (img://18/249m)
wakatta 記法
投稿の仕方
基本的な記法
エンジニア向け記法
記法のサンプル
このコンテンツを見る