[thelist] Finding query string

CDitty mail at redhotsweeps.com
Thu Jul 11 15:44:01 CDT 2002


This is along the lines that I started on.  However, I did 2 searches on
google and the "q" was in a different section.

I could prob do an explode on "q=" instead of just "=".  I'll try that.

Thanks

CDitty

At 03:34 PM 7/11/2002, you wrote:
> > 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&sect=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&sect=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
>
>--
>For unsubscribe and other options, including
>the Tip Harvester and archive of thelist go to:
>http://lists.evolt.org Workers of the Web, evolt !




More information about the thelist mailing list