Home >php教程 >PHP源码 >php获取从百度搜索进入网站的关键词

php获取从百度搜索进入网站的关键词

PHP中文网
PHP中文网Original
2016-06-01 14:32:501148browse

php代码

<?php 

function search_word_from() {
	$referer = isset($_SERVER[&#39;HTTP_REFERER&#39;])?$_SERVER[&#39;HTTP_REFERER&#39;]:&#39;&#39;;
	if(strstr( $referer, &#39;baidu.com&#39;)){ //百度
		preg_match( "|baidu.+wo?r?d=([^\&]*)|is", $referer, $tmp );
		$keyword = urldecode( $tmp[1] );
		$from = &#39;baidu&#39;;
	}elseif(strstr( $referer, &#39;google.com&#39;) or strstr( $referer, &#39;google.cn&#39;)){ //谷歌
		preg_match( "|google.+q=([^\&]*)|is", $referer, $tmp );
		$keyword = urldecode( $tmp[1] );
		$from = &#39;google&#39;;
	}elseif(strstr( $referer, &#39;so.com&#39;)){ //360搜索
		preg_match( "|so.+q=([^\&]*)|is", $referer, $tmp );
		$keyword = urldecode( $tmp[1] );
		$from = &#39;360&#39;;	
	}elseif(strstr( $referer, &#39;sogou.com&#39;)){ //搜狗
		preg_match( "|sogou.com.+query=([^\&]*)|is", $referer, $tmp );
		$keyword = urldecode( $tmp[1] );
		$from = &#39;sogou&#39;;	
	}elseif(strstr( $referer, &#39;soso.com&#39;)){ //搜搜
		preg_match( "|soso.com.+w=([^\&]*)|is", $referer, $tmp );
		$keyword = urldecode( $tmp[1] );
		$from = &#39;soso&#39;;
	}else {
		$keyword =&#39;&#39;;
		$from = &#39;&#39;;
	}

	return array(&#39;keyword&#39;=>$keyword,&#39;from&#39;=>$from);
}

//以下为测试
//在搜索引擎搜索个关键词,进入网站
$word = search_word_from();
if(!empty($word[&#39;keyword&#39;])){
	echo &#39;关键字:&#39;.$word[&#39;keyword&#39;].&#39; 来自:&#39;.$word[&#39;from&#39;];
}
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn