PHP records the keywords that users enter the website through the search engine and provides relevant results

Source: Internet
Author: User
Tags 0xc0 chr ord strlen

The following is the implementation code for accessing your website. For more information, let's expand on your own.

The code is as follows: Copy code

$ Rfr = $ _ SERVER ['http _ referer'];
// If (! $ Rfr) $ rfr = 'http: // '. $ _ SERVER ['http _ host'];

If ($ rfr)
{
$ P = parse_url ($ rfr );
Parse_str ($ p ['query'], $ pa );
$ P ['host'] = strtolower ($ p ['host']);
$ Arr_sd_key = array (
'Baidu. Com' => 'word ',
'Google. Com' => 'Q ',
'Sina .com.cn '=> 'word ',
'Sohu. Com' => 'word ',
'MSN. Com' => 'Q ',
'Bing. Com' => 'Q ',
'2014. Com' => 'Q ',
'Yahoo. Com' => 'P'
);
$ Keyword = '';
$ Sengine = $ p ['host'];
Foreach ($ arr_sd_key as $ se => $ kwd)
 {
If (strpos ($ p ['host'], $ se )! = False)
  {
$ Keyword = $ pa [$ kwd];
$ Sengine = $ se;
Break;
  }
 }
$ SQL = "insert into visit_log (domain, key_word, ct )";
}

The above code does not provide relevant results based on the user's words. Next I will adjust the above functions.

The code is as follows: Copy code

<? Php
Function unescape ($ str ){
$ Ret = '';
$ Len = strlen ($ str );
For ($ I = 0; $ I <$ len; $ I ++ ){
If ($ str [$ I] = '%' & $ str [$ I + 1] = 'u '){
$ Val = hexdec (substr ($ str, $ I + 2, 4 ));
If ($ val <0x7f)
$ Ret. = chr ($ val );
Else
If ($ val <0x800)
$ Ret. = chr (0xc0 | ($ val> 6). chr (0x80 | ($ val & 0x3f ));
Else
$ Ret. = chr (0xe0 | ($ val> 12 )). chr (0x80 | ($ val> 6) & 0x3f )). chr (0x80 | ($ val & 0x3f ));

$ I + = 5;
} Else
If ($ str [$ I] = '% '){
$ Ret. = urldecode (substr ($ str, $ I, 3 ));
$ I + = 2;
} Else
$ Ret. = $ str [$ I];
 }
Return $ ret;
}
Function ls_get_delim ($ ref ){
$ Search_engines = array (
'Google .com.hk '=> 'Q ',
'Google .com.tw '=> 'Q ',
'Go .google.com '=> 'Q ',
'Google. Com' => 'Q ',
'Blogsearch .google.com '=> 'Q ',
'CN .bing.com '=> 'Q ',
'One .cn.yahoo.com '=> 'P ',
'Baidu. Com' => 'wd ',
'Soso. Com' => 'W ',
'Youdao. Com' => 'Q ',
'Sogou. Com' => 'query'
);
$ Delim = false;
// Judge
If (isset ($ search_engines [$ ref]) {
$ Delim = $ search_engines [$ ref];
 }
Return $ delim;
}

Function ls_get_refer (){
// Determine the URL address of the previous page
$ QueryString = $ _ GET ['referer'];
$ QueryString = unescape ($ queryString );
If (! Isset ($ queryString) | ($ queryString = ''))
Return false;
$ Referer_info = parse_url ($ queryString );
$ Referer = $ referer_info ['host'];
// Remove www.
If (substr ($ referer, 0, 4) = 'www .')
$ Referer = substr ($ referer, 4 );
Return $ referer;
}

Function ls_getinfo ($ what ){
$ Referer = ls_get_refer ();
If (! $ Referer)
Return false;
$ Delimiter = ls_get_delim ($ referer );
If ($ delimiter ){
$ Terms = ls_get_terms ($ delimiter );
If ($ what = 'isref '& $ terms! = ''){
Return true;
  }
If ($ what = 'terms '){
Echo $ terms;
  }
 }
Return false;
}

Function yxiao_seems_utf8 ($ str ){
$ Length = strlen ($ str );
For ($ I = 0; $ I <$ length; $ I ++ ){
$ C = ord ($ str [$ I]);
If ($ c <0x80)
$ N = 0; #0 bbbbbbb
Elseif ($ c & 0xE0) = 0xC0) $ n = 1; #110 bbbbb
Elseif ($ c & 0xF0) = 0xE0) $ n = 2; #1110 bbbb
Elseif ($ c & 0xF8) = 0xF0) $ n = 3; # 11110bbb
Elseif ($ c & 0xFC) = 0xF8) $ n = 4; # 111110bb
Elseif ($ c & 0xFE) = 0xFC) $ n = 5; # 1111110b
Else
Return false; # Does not match any model
For ($ j = 0; $ j <$ n; $ j ++) {# n bytes matching 10 bbbbbb follow?
If (+ + $ I = $ length) | (ord ($ str [$ I]) & 0xC0 )! = 0x80 ))
Return false;
  }
 }
Return true;
}

Function ls_get_terms ($ d ){
// Obtain the query value
$ QueryString = $ _ GET ['referer'];
$ QueryString = unescape ($ queryString );
$ Query_str = parse_url ($ queryString );
Parse_str ($ query_str [query], $ query_str );
$ Query = $ query_str [$ d];
$ Query = urldecode ($ query );

$ Query = str_replace ("'", '', $ query );
$ Query = str_replace ('"','', $ query );
$ Query_array = preg_split ('/[s, +.] +/', $ query );
$ Query_terms = implode ('', $ query_array );
$ Terms = htmlspecialchars ($ query_terms );
// Gbk-> utf8
If (! Yxiao_seems_utf8 ($ terms )){
$ Terms = iconv ("GBK", "UTF-8 // IGNORE", $ terms );
 }

Return $ terms;
}
If (ls_getinfo ('isref ')){
?>
Document. write ('<div id = "serp"> <p> ')
Document. write ('more search results: <em> <a href = "/search? Q = <? Php ls_getinfo (terms);?> "')
Document. write ('title = "View <? Php ls_getinfo (terms);?> Search Result "rel =" nofollow "> <? Php ls_getinfo (terms);?> </A> </em> ')
Document. write ('</p> </div> ');
<? Php }?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.