How to retrieve http header info

by 5 replies
6
I have a site that gets good traffic from Google and I want to track what keywords are converting from the natural searches.

Can anyone tell me how I can use php to grab the keyword information when someone lands on my page after finding it on a search engine.

I will then attach that keyword to a variabl (I can do this part )

Cheers,
Dave
#programming #header #http #info #retrieve
  • Hi you can grab a url string variable sent through google and store it in a msql database.

    Look for the this code in the url string. "www.google.co.za/search?q=make+money&ie=utf-8&oe=utf-8&aq=t&client=firefox-a&rlz=1R1GGGL_en___ZA347"

    where q=make+money is what you want to store

    so you will get the variable by doing the following

    $keyword = $_GET['q'];

    This will store the search term in a variable called "keyword" now you just need to store the variable data in the in the mysql database table.

    Hope this is useful?
  • That's not really a helpful solution, and is actually incorrect. The $_GET variables are only available if they are present in YOUR website URL.

    I believe the only way to do this is with the "referer" (notice the incorrect spelling, it is actually correct). The referer header doesn't always get sent by the browser, but it is usually there.

    http://www.electrictoolbox.com/php-h...erer-variable/

    You should then dissect the url string to find what keyword is being searched for. The search term in a Google search looks something like this:
    Code:
    http://www.google.com/search?q=hello+world&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
    Here, I searched for "hello world". So, the term is in the "q=hello+world" part. You will need the PHP function urldecode to get the literal string. To get that string, you will need to explode the url string, first at the "?" deliminator, then at the "&" delimiter. Then, cycle through the second array, and when you have the string "q=" at the beginning of an item, remove it, and what is left is the search term. This sounds complicated, but it isn't that bad.
    • [1] reply
    • Wayfarer is correct. You have to look at the REFERER. I found this code here. It seems to work fine in a quick test.

      PHP Code:
      $parsed_url=parse_url($_SERVER['HTTP_REFERER']);
      if(
      stripos($parsed_url['host'],'google'))
      {
      parse_str($parsed_url['query'],$parsed_query_string);
      $keyword_string=$parsed_query_string['q'];

      Steve
      • [1] reply

Next Topics on Trending Feed

  • 6

    I have a site that gets good traffic from Google and I want to track what keywords are converting from the natural searches. Can anyone tell me how I can use php to grab the keyword information when someone lands on my page after finding it on a search engine.