萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP通過訪客來路獲取搜索關鍵詞的方法

PHP通過訪客來路獲取搜索關鍵詞的方法

<?php

class keyword{

	public function getKeyword($referer){
		if(strpos($referer,"http://www.baidu.com")> -1 ){
			$keyword = $this->getbaidukeyword($referer);
		}else if(strpos($referer,"http://www.google.com")> -1 ){
			$keyword = $this->getgooglekeyword($referer);
		}else if(strpos($referer,"http://www.soso.com")> -1 ){
			$keyword = $this->getsosokeyword($referer);
		}else if(strpos($referer,"http://www.sogou.com")> -1 ){
			$keyword = $this->getsogoukeyword($referer);
		}
		
		return $keyword;
	}
	
	//由來路取得百度關鍵詞
	private function getbaidukeyword($str){
		$s = strpos($str,'wd=');
		if($s>-1){
			$str = substr($str,$s+3);
			$e = strpos($str,'&');
			if($e>-1){
				$str = substr($str,0,$e);
			}
			$str = rawurldecode($str);
		}
		return $str;
	}
	//獲得谷歌關鍵詞
	private function getgooglekeyword($str){
		$s = strpos($str,'&q=');
		if($s>-1){
			$str = substr($str,$s+3);
			$e = strpos($str,'&');
			if($e>-1){
				$str = substr($str,0,$e);
			}
			$str = rawurldecode($str);
		}
		return $str;
	}
	//獲得SOSO關鍵詞
	private function getsosokeyword($str){
		$s = strpos($str,'?w=');
		if($s>-1){
			$str = substr($str,$s+3);
			$e = strpos($str,'&');
			if($e>-1){
				$str = substr($str,0,$e);
			}
			$str = rawurldecode($str);
		}else{
			$s = strpos($str,'&w=');
			if($s>-1){
				$str = substr($str,$s+3);
				$e = strpos($str,'&');
				if($e>-1){
					$str = substr($str,0,$e);
				}
				$str = rawurldecode($str);
			}
		}
		return $str;
	}
	//獲得sogou關鍵詞
	private function getsogoukeyword($str){
		$s = strpos($str,'query=');
		if($s>-1){
			$str = substr($str,$s+6);
			$e = strpos($str,'&');
			if($e>-1){
				$str = substr($str,0,$e);
			}
			$str = rawurldecode($str);
		}
		return $str;
	}

}



?>
copyright © 萬盛學電腦網 all rights reserved