I am using a script to try and capture the keywords used to access my website. I can't seem to find the problem. I am taking the returned value and storing it in a mySQL database, but when I test it I just fet 'Array' stored in the DB. Here is the script I am using, where am I going wrong? function check_se_keywords() { $refer = parse_url($_SERVER['HTTP_REFERER']); $host = $refer['host']; $refer = $refer['query']; if(strstr($host,'google')) { //do google stuff $match = preg_match('/&q=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('&q=','',$querystring); $keywords = explode('+',$querystring); return $keywords; } elseif(strstr($host,'yahoo')) { //do yahoo stuff $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('p=','',$querystring); $keywords = explode('+',$querystring); return $keywords; } elseif(strstr($host,'msn')) { //do msn stuff $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('q=','',$querystring); $keywords = explode('+',$querystring); return $keywords; } else { //else, who cares return false; } } PHP:
straight from the explode manual page : Returns an array of strings http://us2.php.net/explode try something like $keyword_list = implode(",",$keywords); return $keyword_list; should give keyword1,keyword2 etc.
$keywords is an array, if you do return $keywords then all it will give is "Array" because no array value is defined. do this: function check_se_keywords($input) { $refer = parse_url($input); $host = $refer['host']; $refer = $refer['query']; if(strstr($host,'google')) { //do google stuff $match = preg_match('/&q=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('&q=','',$querystring); $keywords = explode('+',$querystring); return $keywords[0]; } elseif(strstr($host,'yahoo')) { //do yahoo stuff $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('p=','',$querystring); $keywords = explode('+',$querystring); return $keywords[0]; } elseif(strstr($host,'msn')) { //do msn stuff $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('q=','',$querystring); $keywords = explode('+',$querystring); return $keywords[0]; } else { //else, who cares return false; } } PHP:
Cheers. I have it working now like this: function check_se_keywords() { $refer = parse_url($_SERVER['HTTP_REFERER']); $host = $refer['host']; $refer = $refer['query']; if(strstr($host,'google')) { //do google stuff $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('q=','',$querystring); $keywords = explode('+',$querystring); $keywords = implode(' ', $keywords); return $keywords; } elseif(strstr($host,'yahoo')) { //do yahoo stuff $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('p=','',$querystring); $keywords = explode('+',$querystring); $keywords = implode(' ', $keywords); return $keywords; } elseif(strstr($host,'msn')) { //do msn stuff $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('q=','',$querystring); $keywords = explode('+',$querystring); $keywords = implode(' ', $keywords); return $keywords; } elseif(strstr($host,'bing')) { //do bing stuff $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('q=','',$querystring); $keywords = explode('+',$querystring); $keywords = implode(' ', $keywords); return $keywords; } elseif(strstr($host,'ask')) { //do ask jeeves stuff $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('q=','',$querystring); $keywords = explode('+',$querystring); $keywords = implode(' ', $keywords); return $keywords; } else { //else, who cares return false; } } PHP:
Glad, you got it working although, you don't really need an extra line of code when you could just do return $keywords[0]; Code (markup):
And instead of this: $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('p=','',$querystring); PHP: You can just do this (change 0 to 1): $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[1]; PHP:
Im using this one, give it a try $referrer = $_SERVER['HTTP_REFERER']; if(preg_match("/[\.\/](google|yahoo|bing|geegain|mywebsearch|ask|alltheweb)\.[a-z\.]{2,5}[\/]/i",$referrer,$search_engine)){ $referrer_query = parse_url($referrer); $referrer_query = $referrer_query["query"]; $q = "[q|p]"; //Yahoo uses both query strings, I am using switch() for each search engine preg_match("/".$q."=(.*?)&/",$referrer,$keyword); $keyword = urldecode($keyword[1]); echo $keyword; } PHP:
I have found a problem that I could use some help with. My script is working well at picking out keywords, or so I thought. After looking through my data in my database I have discovered that users who search for my domain name don't get caught properly. Example: Search google for "www.eaguingamp.co.uk" it sends the following to my http_referer: http://www.google.co.uk/url?sa=t&so...R4nnNtuJtyznAilZQ&sig2=175q1KTF36lsLf-7zRRr9g When this comes to my database all that gets stored is 'www' it is ignoring everything after the '.'. Here is what I am processing the string with //do google stuff $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output); $querystring = $output[0]; $querystring = str_replace('q=','',$querystring); $keywords = explode('+',$querystring); $keywords = implode(' ', $keywords); return $keywords; PHP: How can I fix this problem?
@noname0 your script is really great! - How can the keywords be grouped and sorted? Thanks in advance