Jump to content

Parsing HTTP Referer using RegExp


sufyaaan

Recommended Posts

Hi all,

 

I'm using the following code to track traffic to my site from search engines. This code works alright.

 

As you can see, I'm parsing 'HTTP REFERER' to get 'search engine' and 'keywords'.

 

The problem is when the HTTP REFERER is one of the following:

 

http://www.google.com/search?sourceid=navclient&aq=1&oq=php+li&ie=UTF-8&rlz=1T4ADFA_enUS427US433&q=php+tutorial

 

http://www.google.com/m/search?aq=f&oq=&aqi=g6-k0d0t0&fkt=352&fsdt=22941&cqt=&rst=&htf=&his=&maction=&q=php+help+&flip=0

 

http://search.yahoo.com/search;_ylt=At9KK_hlAGOurdCBDM1VS1ebvZx4?vc=&fp_ip=ph&p=php+tips&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701

 

My code cannot parse 'keywords'. I don't know why.

 

Can anyone here please help me?

 

	function track(){

if(preg_match('/[\.\/](google|yahoo|bing|ask|aol)\.[a-z\.]{2,5}[\/]/i', $_SERVER['HTTP_REFERER'], $search_engine)){

	$parsed_query = parse_url($row['referrer']);

	$parsed_query = $parsed_query['query'];

	$q = "[q|p]";

	preg_match('/'.$q.'=(.*?)&/', $_SERVER['HTTP_REFERER'], $keyword);

	$se = parse_host($search_engine[0]);

	$m_keywords = strtolower(urldecode($keyword[1]));

	$m_search_engine  = ucfirst(substr(strtolower($se),1,strlen($se)));		

}

}


function parse_host($address) { 

   $parseUrl = parse_url(trim($address)); 
   return trim($parseUrl['host'] ? $parseUrl['host'] : array_shift(explode('/', $parseUrl['path'], 2)));
    
}

Link to comment
https://forums.phpfreaks.com/topic/243400-parsing-http-referer-using-regexp/
Share on other sites

okay here is what I came up with for you...

 

as far as your search engine regex is concerned..all you really need to do is search for the words google, yahoo, bing etc...

all of the other regex code is not really necessary...

 

Now to your keywords issue, here is the code that I came up with that will isolate the p or q value

 

$pattern = '~&[q|p]=([^&]*)~'

hope this helps

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.