Is it possible to create a script in php (or other) that could target a banner or other advertising to people coming from google through a certain keyword? I want to show the banner for one product when people people visit my site through "Keyword A" to see one banner, and the people coming from "keyword b" to see a completely different banner ad. Like targeting the ads by what the visitor is searching for. Can this be done?
Yes, you can utilize $_SERVER['HTTP_REFERER'] in this way. For example a query 'video converter download' was searched, then the url will be something like: "http://www.google.com/search?sourceid=navclient&aq=t&ie=UTF-8&rlz=1T4GGLJ_enPK279PK279&q=video+converter+download" Now when it comes to your script: $info = parse_url($_SERVER['HTTP_REFERER']); if($info['host'] == 'www.google.com') { #good, utilize the keywords $pos = strpos($info['query'],"q="); if($pos === FALSE) { #handle error, no keywords } $keywords = substr($info['query'],$pos+2,1024); print $keywords; } Code (markup): It will probably print something like I hope it helps. regards
Here you go, I just wrote this quickly. Put this at the top of your file: <?php // Change this to your default banner if there are no keywords $sBanner = '<img src="default.gif">'; // Change this to suit, the format is: 'keyword' => 'output' $aBanners = array('test' => '<img src="test.gif">', 'test2' => '<img src="test2.gif">' ); if ( isset($_SERVER['HTTP_REFERER']) ) { $aUrl = parse_url($_SERVER['HTTP_REFERER']); switch($aUrl['host']) { case 'search.yahoo.com': $sVar = 'p'; case 'search.live.com': case 'www.google.com': case 'google.com': $sVar = 'q'; break; } if ( isset($sVar) ) { $aQuery = explode('&', $aUrl['query']); foreach($aQuery as $sQuery) { list($sName, $sValue) = explode('=', $sQuery); if ( $sName == $sVar ) { $aKeywords = explode('+', $sValue); } } foreach($aKeywords as $sKeyword) { if ( array_key_exists($sKeyword, $aBanners) ) { $sBanner = $aBanners[$sKeyword]; break; } } } } ?> PHP: Then where you want your banner displayed place this: <?php echo $sBanner; ?> PHP: I hope that helps .
That looks awesome! cant wait to try it. But a question. Now if a user visits from google initially through a keyword, but then clicks on another page inside my site. Will the visitor still see the correct ad if the referer is technically different? Thanks!
clarky: Nice script, but perhaps instead of exploding by & and =, use the parse_str function designed for this, I would definitely recommend you use the second parameter. Here's a link: http://php.net/manual/en/function.parse-str.php