Sometimes, when we enter a website from a search engine, there will be a line of small words, "welcome from Google/Baidu, by retrieving the" XXX "keyword to this site" and so on. How can this function be implemented? In fact, it is not difficult. The general idea is to obtain the inbound URL and repeat the data we need.
<? Php $ url = isset ($ _ SERVER ['HTTP _ referer'])? $ _ SERVER ['HTTP _ referer']: ''; // get the inbound url. $ Search_1 = "google.com"; // q = $ search_2 = "baidu.com"; // wd = $ google = preg_match ("/\ B {$ search_1} \ B /", $ url); // record matching information for inbound judgment. $ Baidu = preg_match ("/\ B {$ search_2} \ B/", $ url); $ s_s_keyword = ""; if ($ google) {// from google $ s_s_keyword = get_keyword ($ url, 'q = '); // The character before the keyword is "q = ". $ S_s_keyword = urldecode ($ s_s_keyword); // $ s_s_keyword = iconv ("GBK", "UTF-8", $ s_s_keyword); // The engine is gbk} else if ($ baidu) {// from Baidu $ s_s_keyword = get_keyword ($ url, 'wd = '); // The character before the keyword is "wd = ". $ S_s_keyword = urldecode ($ s_s_keyword); $ s_s_keyword = iconv ("GBK", "UTF-8", $ s_s_keyword); // The engine is gbk} echo '$ s_s_keyword '; /* obtain the keywords from the search engine's inbound traffic. * // Function: extract keywords from URLs. Parameter description: the url and the characters before the keyword. Function get_keyword ($ url, $ kw_start) {$ start = stripos ($ url, $ kw_start); $ url = substr ($ url, $ start + strlen ($ kw_start )); $ start = stripos ($ url, '&'); if ($ start> 0) {$ start = stripos ($ url ,'&'); $ s_s_keyword = substr ($ url, 0, $ start);} else {$ s_s_keyword = substr ($ url, 0);} return $ s_s_keyword;}?>