[thelist] Finding query string
David Bindel
dbindel at austin.rr.com
Thu Jul 11 15:35:00 CDT 2002
> Does anyone have any php code snippets that allows you to find the query
> string the is located in the $http_referer variable? Basically, I am
> looking for something that will give me what the user searched for from
> various search engines.
It would be kind of hard to make this perfect because every search engine
has a different way of generating their search query string, but here's my
idea:
$urlparts = explode("?", $http_referer);
$querystring = $urlparts[1];
Of course this would set $querystring to something like
"srchterm=What+the+user+searched+for&pg=54§=2" so it's a very primitive
way of finding out. You improve the readability by changing it to this:
$urlparts = explode("?", $http_referer);
$querystring = urldecode($urlparts[1]);
urldecode decodes any %## encoding and +'s in the string, but you will still
have to worry about sorting out the variables. The variable $querystring
will now look something like this: "srchterm=What the user searched
for&pg=54§=2". You could now split it by the "&"'s which appear in it:
$urlparts = explode("?", $http_referer);
$querystring = urldecode($urlparts[1]);
$qsparts = explode("&", $querystring);
Which will give you the following array:
$qsparts[0] = "srchterm=What the user searched for"
$qsparts[1] = "pg=54"
$qsparts[2] = "sect=2"
If you wanted to take it one step further, you could extract these variable
names and values from the querystring and make them into variables which can
be access by PHP:
$urlparts = explode("?", $http_referer);
$querystring = urldecode($urlparts[1]);
$qsparts = explode("&", $querystring);
extract($qsparts);
Which would create the following variables for access by PHP:
$srchterm = "What the user searched for"
$pg = "54"
$sect = "2"
Remember though that each search engine will return a different set of
variables for this, so you may have to write a bunch of if... then
conditionals to figure out which search engine was being used.
Good luck!
David Bindel
More information about the thelist
mailing list