本文實例講述了php實現判斷訪問來路是否為搜索引擎機器人的方法。分享給大家供大家參考。具體分析如下:
很多時候我們需要對網站訪客來路進行識別,針對真實用戶與搜索引擎作不同動作實現,那么首先就需要判斷是否為搜索引擎。
php判斷方法非常簡單,通過過濾$_SERVER['HTTP_USER_AGENT'] 參數即可進行識別,以下是摘錄某開源程序的相關源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
private function getRobot() { if ( empty ( $_SERVER [ 'HTTP_USER_AGENT' ])) { return false; } $searchEngineBot = array ( 'googlebot' => 'google' , 'mediapartners-google' => 'google' , 'baiduspider' => 'baidu' , 'msnbot' => 'msn' , 'yodaobot' => 'yodao' , 'youdaobot' => 'yodao' , 'yahoo! slurp' => 'yahoo' , 'yahoo! slurp china' => 'yahoo' , 'iaskspider' => 'iask' , 'sogou web spider' => 'sogou' , 'sogou push spider' => 'sogou' , 'sosospider' => 'soso' , 'spider' => 'other' , 'crawler' => 'other' , ); $spider = strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ]); foreach ( $searchEngineBot as $key => $value ) { if ( strpos ( $spider , $key )!== false) { return $value ; } } return false; } public function isRobot() { if ( $this ->getRobot()!==false) { return true; } return false; } |
希望本文所述對大家的php程序設計有所幫助。