How to retrieve http header info

5 replies
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
#header #http #info #retrieve
  • Profile picture of the author holla22
    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?
    {{ DiscussionBoard.errors[1236615].message }}
  • Profile picture of the author wayfarer
    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.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[1236766].message }}
    • Profile picture of the author Steve Diamond
      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
      Signature
      Mindfulness training & coaching online
      Reduce stress | Stay focused | Keep positive and balanced
      {{ DiscussionBoard.errors[1236929].message }}
      • Profile picture of the author holla22
        Oh well that is the way I'm doing it and it works fine for me. Yes you obviously want to see who the referrer is like google, yahoo, bing etc, but that was just a simple quick explanation. I just thought that you already knew the rest
        {{ DiscussionBoard.errors[1237480].message }}
        • Profile picture of the author DavidMeade
          hey fellas,

          thanks for your help with this, much appreciated.

          Dave
          {{ DiscussionBoard.errors[1237751].message }}

Trending Topics