MainStWebGuy Posted March 24, 2009 Share Posted March 24, 2009 HEy guys, I was going over some of the google webmaster tools and analytics and started wondering... Is there a way for me to know what search string a user enters to find my site? I'm pretty new to using php and came across this code snippet that i thought was interesting... does this look like something that will work? $referer = $_SERVER[HTTP_REFERER]; if($referer) { preg_match("/[\&\?]q=([^&]*)/", $referer, $matches); if($matches[1]) { $search_query = rawurldecode($matches[1]); $search_query = str_replace("+", " ", $search_query); } } print "You searched Google for '$search_query'"; I'd love to hear what some of you gurus think! thanks! J Quote Link to comment https://forums.phpfreaks.com/topic/150912-http_referer/ Share on other sites More sharing options...
JonnoTheDev Posted March 24, 2009 Share Posted March 24, 2009 It is your web server log files that should be analysed for search engine keywords. Look at AWStats, Webalizer, etc.. however the $_SERVER['HTTP_REFERER'] will contain the url of the page that referred the user. This part is looking at the url to see if it matches a Google query string: preg_match("/[\&\?]q=([^&]*)/", $referer, $matches); The keyword is then extracted. http://awstats.sourceforge.net/ http://www.webalizer.org/ Quote Link to comment https://forums.phpfreaks.com/topic/150912-http_referer/#findComment-792800 Share on other sites More sharing options...
MainStWebGuy Posted March 24, 2009 Author Share Posted March 24, 2009 Thanks Neil! I'm following that line of thought and would like to am running into a problem. I've modified the code as follows to check and see if the user entered a particular city in their search string and to assign values to a "cityName" variable appropriately. $referer = $_SERVER[HTTP_REFERER]; if($referer) { preg_match("/[\&\?]q=([^&]*)/", $referer, $matches); if($matches[1]) { $search_query = rawurldecode($matches[1]); $search_query = str_replace("+", " ", $search_query); } if (stristr($search_query, 'city1') == TRUE) { $cityName = "City1"; } elseif (stristr($search_query,'city2') == TRUE) { $cityName = "City2"; } elseif (stristr($search_query, 'city3') == TRUE) { $cityName = "City3"; } elseif (stristr($search_query, 'city4') == TRUE) { $cityName = "City4"; } else { $cityName = "City1, City2, City3, City4 or any other community in between"; } } This all works well, but then i started thinking... what if the visitor reached my page by clicking on a link on another page of my site, or even a link from a referring site? If this happens, would a value to my $cityName variable be assigned at all? If not, can any of you think of a way to assign one? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/150912-http_referer/#findComment-793036 Share on other sites More sharing options...
JonnoTheDev Posted March 25, 2009 Share Posted March 25, 2009 No because this line is looking for a Google search string in the referer preg_match("/[\&\?]q=([^&]*)/", $referer, $matches); if($matches[1]) { You would have to set the value preg_match("/[\&\?]q=([^&]*)/", $referer, $matches); if($matches[1]) { } else { // not a google referer $cityName = "abc"; } Quote Link to comment https://forums.phpfreaks.com/topic/150912-http_referer/#findComment-793441 Share on other sites More sharing options...
MainStWebGuy Posted March 25, 2009 Author Share Posted March 25, 2009 Man i sure do appreciate the tips Neil! I'll change what i had. THanks again! Quote Link to comment https://forums.phpfreaks.com/topic/150912-http_referer/#findComment-793521 Share on other sites More sharing options...
MainStWebGuy Posted March 25, 2009 Author Share Posted March 25, 2009 I hope i'm don't seem lazy with this next question... i'm just having a hard time getting my head around it: First of all... your suggestion worked perfectly, thanks again! Now if a person comes to my site from Google or if they come from a page within my site, the location will be set. Woot woot! However, if they are coming from a referring site, i can't think of a way to assign this location. Now i'm just thinking out loud here, but hypothetically, it would be impossible to know which city to serve if they come from a referring site (or type in the url directly) wouldn't it? would it make sense to add something like <?php ... } else { cityName = $_GET['location'] } if ($_GET['location'] == FALSE) { $cityName = "Default City"; } Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/150912-http_referer/#findComment-793523 Share on other sites More sharing options...
JonnoTheDev Posted March 25, 2009 Share Posted March 25, 2009 With this code preg_match("/[\&\?]q=([^&]*)/", $referer, $matches); if($matches[1]) { $cityName = "xyz"; } else { // not a google referer $cityName = "abc"; } If the referer is from google then $cityName will be xyz. If they are refered from anywhere else including your own site then it is set to abc. If you have the city name in the url then you must bypass this code so it does not change the $cityName variable. i.e. $cityName = (strlen($_GET['location'])) ? $_GET['location'] : false; // no location passed through url - check the referer if(!$cityName) { preg_match("/[\&\?]q=([^&]*)/", $referer, $matches); if($matches[1]) { $cityName = "xyz"; } else { // not a google referer $cityName = "abc"; } } Quote Link to comment https://forums.phpfreaks.com/topic/150912-http_referer/#findComment-793524 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.