Submit new
Join Quickr
Login
Enter a description of the question
Remove
Remove
Remove
Remove
Add another description
Tags users might search with (optional, seperated by space or ,)
Solution
Remove
Source url (optional)
Enter and edit the content of your answer below in the editor.
<p>This first function (getReferalHost) simply obtains the referal URL and uses the PHP function parse_url() to break things down. Then it's a simple set of 'if' statements to return a user-friendly hostname. So whereas the hostname might be images.google.com, I'm using "strstr" to check whether the string 'google' is in the URL, and so on. It's not foolproof but it's okay.<br /> <br /> <br /> <font color="#ff0000">function getReferalHost()<br /> {<br /> $refer = parse_url($_SERVER['HTTP_REFERER']);<br /> $host = $refer['host'];<br /> <br /> if(strstr($host,'google'))<br /> {<br /> return 'Google';<br /> }<br /> elseif(strstr($host,'yahoo'))<br /> {<br /> return 'Yahoo';<br /> }<br /> elseif(strstr($host,'bing'))<br /> {<br /> return 'Bing';<br /> }<br /> }<br /> </font><br /> This next function (getKeywords) parses the referal URL again (not best practice is it? Shame on me). Then I've got a specific preg_match for each of the different vendors (based on the URLs that each of them uses. Google will format their query as such: http://www.google.com/search?&rls=en<strong>&q=seopher+another+keyword</strong> so I know that what I'm after starts with '&q=' with the keywords separated by '+'. So the regex used matches that. Yahoo and MSN do exactly the same, just with slightly different syntax - hence the need for the 'if' statement.<br /> <br /> <br /> <font color="#ff0000">function getKeywords()<br /> {<br /> $refer = parse_url($_SERVER['HTTP_REFERER']);<br /> $host = $refer['host'];<br /> $refer = $refer['query'];<br /> <br /> if(strstr($host,'google'))<br /> {<br /> //do google stuff<br /> $match = preg_match('/&q=([a-zA-Z0-9+-]+)/',$refer, $output);<br /> $querystring = $output[0];<br /> $querystring = str_replace('&q=','',$querystring);<br /> $keywords = explode('+',$querystring);<br /> return $keywords;<br /> }<br /> elseif(strstr($host,'yahoo'))<br /> {<br /> //do yahoo stuff<br /> $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output);<br /> $querystring = $output[0];<br /> $querystring = str_replace('p=','',$querystring);<br /> $keywords = explode('+',$querystring);<br /> return $keywords;<br /> <br /> }<br /> elseif(strstr($host,'bing'))<br /> {<br /> //do msn stuff<br /> $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output);<br /> $querystring = $output[0];<br /> $querystring = str_replace('q=','',$querystring);<br /> $keywords = explode('+',$querystring);<br /> return $keywords;<br /> }<br /> else<br /> {<br /> //else, who cares<br /> return false;<br /> }<br /> }<br /> </font></p> <p>Really you can add as many different vendors to this function, I'm just focusing on the obvious 3 because they constitute about 98%+ of my search traffic.</p> <p>This final function is what I'm using to write the recommendations; it takes the keywords used as a parameter and uses MySQL scoring to deliver results... So it takes the search terms and tries to find similar content on the website:<br /> <br /> <br /> <font color="#ff0000">function getSearchHelperList($term, $limit, $currentPageID)<br /> {<br /> $searchterm = $term;<br /> <br /> //a function I use to sanitise all inputs<br /> $searchterm = $this->cleanInput($searchterm);<br /> <br /> /*query that takes the searchterms and scores it against the articles<br /> it also checks that the title isn't the same as the page we're already on<br /> because we don't want it showing you a link to your current page*/<br /> $sql = "SELECT CatID, Title, Content, CleanTitle, UNIX_TIMESTAMP(TIME) AS FORMATED_TIME, MATCH (Title, Content) AGAINST ('$searchterm') AS score FROM Articles WHERE MATCH (Title, Content) AGAINST ('$searchterm') AND CleanTitle != '$currentPageID' AND Time < CURRENT_TIMESTAMP ORDER BY score DESC";<br /> <br /> $result = mysql_query($sql);<br /> <br /> $num = mysql_numrows($result);<br /> <br /> if($num < $limit)<br /> {<br /> $limit = $num;<br /> }<br /> <br /> //old school right? for loops! <br /> //build an unordered list of related content<br /> if($limit > 0)<br /> {<br /> echo '<ul>';<br /> for($i=0; $i<$limit; $i++)<br /> {<br /> echo '<li><a href="/articles/'.mysql_result($result,$i,'CleanTitle').'">';<br /> echo mysql_result($result,$i,'Title');<br /> echo '</a></li>';<br /> }<br /> echo '</ul>';<br /> <br /> }<br /> else<br /> {<br /> //display a message<br /> }<br /> }</font></p>
Add another solution
Select a category for your story
Web Programming
Html
Xml
Javascript
Ajax
Php
Asp
Mysql
Database
Server
Programming
C/C++
Visual C++
Java
C#
VB.NET
VB6
Python
Perl
Ruby
Delphi
Computer
Internet
Windows
Hardware
Software
Linux
Antivirus
Shopping
Computer
Electronics
Health
Medication and Drugs
Diet & Nutrition
Business & Finance
Business
Travel
Air travel
Quickr
FAQ
Other
Other
Public
Private
If you want to share your answer with other users, please choose public.