Results 1 to 13 of 13

Thread: Recording incoming keywords from google searches.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Posts
    395

    Talking Recording incoming keywords from google searches.

    I am trying to record incoming keywords from google searches.

    So far I have this code (Im new)

    Code:
    <?php
    echo $_SERVER['HTTP_REFERER'];
    ?>

    For example, this is where I would grab the keyword from -

    http://search.yahoo.com/search?p=KEY...=UTF-8&fr=moz2

    Can someone provide me with the code to do this?

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Recording incoming keywords from google searches.

    I'm sure you could do this with regular expressions, but I'm guessing you have no idea how they work. instead, you can do something simple like this to ignore the un-needed URL stuffs:
    PHP Code:
    <pre>
    <?php
      
    /*
       * grab our referer and see if we need to parse it
       * (we don't want to waste time parsing it if it's empty)
       */ 
      
    $r $_SERVER['HTTP_REFERER'];

      if(
    $r != ""){
        
    $p parse_url($r);
        
    /*
         * make all parameter values lowercase for easy comparing
         */
        
    foreach($p as $key => $value)
          
    $p[$key] = strtolower($value);

        
    /*
         * see if they're using a search engine we recognize
         */
        
    if($p['host'] == "www.google.com" && $p['path'] == "/search" ||
           
    $p['host'] == "search.yahoo.com" && substr($p['path'], 07) == "/search"){

          
    /*
           * determine which host we have, so that we can
           * grab the correct parameter of the query string
           */
          
    switch($p['host']){
            case 
    "www.google.com"$k "q"; break;
            case 
    "search.yahoo.com"$k "p"; break;
          }
          
    /*
           * loop through each parameter in the query string
           * to find the one we want (defined as $k above)
           */
          
    $params explode('&'$p['query']);
          
    $i 0;
          while(
    $i count($params)){
            
    $v split('='$params[$i]);
            if(
    $v[0] == $k){
              
    /*
               * this is the parameter we wanted, let's decode it
               * and break out of this loop
               */
              
    $keywords urldecode($v[1]);
              break;
            }
            
    $i++;
          }
        }
      }
      if(isset(
    $keywords)){
        echo 
    "We've determined you've reached this page via a search engine!\n";
        echo 
    'The keywords you were searching for were: "<strong>' $keywords '</strong>"';
      }else
        echo 
    "You didn't reach this page via a search engine!";
    ?>
    </pre>
    I made referers from yahoo and google both work in that example. if you want to add any more, you'll have to add them yourself.

    edit: revised code just a little.
    Last edited by kows; Jan 27th, 2007 at 03:19 PM.

  3. #3
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Recording incoming keywords from google searches.

    Quote Originally Posted by kows
    I'm sure you could do this with regular expressions, but I'm guessing you have no idea how they work. instead, you can do something simple like this to ignore the un-needed URL stuffs:
    PHP Code:
    <pre>
    <?php
      
    /*
       * grab our referer and see if we need to parse it
       * (we don't want to waste time parsing it if it's empty)
       */ 
      
    $r $_SERVER['HTTP_REFERER'];

      if(
    $r != ""){
        
    $p parse_url($r);
        
    /*
         * make all parameter values lowercase for easy comparing
         */
        
    foreach($p as $key => $value)
          
    $p[$key] = strtolower($value);

        
    /*
         * see if they're using a search engine we recognize
         */
        
    if($p['host'] == "www.google.com" && $p['path'] == "/search" ||
           
    $p['host'] == "search.yahoo.com" && substr($p['path'], 07) == "/search"){

          
    /*
           * determine which host we have, so that we can
           * grab the correct parameter of the query string
           */
          
    switch($p['host']){
            case 
    "www.google.com"$k "q"; break;
            case 
    "search.yahoo.com"$k "p"; break;
          }
          
    /*
           * loop through each parameter in the query string
           * to find the one we want (defined as $k above)
           */
          
    $params explode('&'$p['query']);
          
    $i 0;
          while(
    $i count($params)){
            
    $v split('='$params[$i]);
            if(
    $v[0] == $k){
              
    /*
               * this is the parameter we wanted, let's decode it
               * and break out of this loop
               */
              
    $keywords urldecode($v[1]);
              break;
            }
            
    $i++;
          }
        }
      }
      if(isset(
    $keywords)){
        echo 
    "We've determined you've reached this page via a search engine!\n";
        echo 
    'The keywords you were searching for were: "<strong>' $keywords '</strong>"';
    //RIGHT HERE 
      
    }else {
        echo 
    "You didn't reach this page via a search engine!";
    ?>
    </pre>
    I made referers from yahoo and google both work in that example. if you want to add any more, you'll have to add them yourself.

    edit: revised code just a little.
    you forgot a open bracket on the last else. i added it in the quote
    My usual boring signature: Something

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Posts
    395

    Re: Recording incoming keywords from google searches.

    Nice! Now if I could just record the keywords to either a flat file or database and display them within a page that would be so awesome.

  5. #5
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Recording incoming keywords from google searches.

    PHP Code:
    $sql "INSERT INTO `searches` SET date='".date()."', keywords='".$keywords."'";
    $query mysql_query($sql) or DIE(mysql_error()); 
    That will work with Kows' code.
    My usual boring signature: Something

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Recording incoming keywords from google searches.

    you forgot a open bracket on the last else. i added it in the quote
    umm, no -- I didn't miss anything. the change you made in that quote will actually cause a fatal error because I don't use any curly brackets on that else. that echo line terminates the else statement, because it's the first statement after the "else." if I were to put multiple lines after that else, though, it will always run those lines unless the statements were enclosed in curly brackets.

    for example, this works fine (of course, the lines can't all just be comments):
    PHP Code:
    if(statement){
        
    //multiple lines
        //multiple lines
        //multiple lines
        //multiple lines
      
    }else
        
    //one line 
    while what you changed wouldn't:
    PHP Code:
    if(statement){
        
    //multiple lines
        //multiple lines
        //multiple lines
        //multiple lines
      
    }else{
        
    //one line 
    but, if you did add an extra curly bracket at the very bottom, it would run fine.. but it's a waste of 2 characters if you're not doing anything else with that statement.

  7. #7
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Recording incoming keywords from google searches.

    Quote Originally Posted by kows
    umm, no -- I didn't miss anything. the change you made in that quote will actually cause a fatal error because I don't use any curly brackets on that else. that echo line terminates the else statement, because it's the first statement after the "else." if I were to put multiple lines after that else, though, it will always run those lines unless the statements were enclosed in curly brackets.

    for example, this works fine (of course, the lines can't all just be comments):
    PHP Code:
    if(statement){
        
    //multiple lines
        //multiple lines
        //multiple lines
        //multiple lines
      
    }else
        
    //one line 
    while what you changed wouldn't:
    PHP Code:
    if(statement){
        
    //multiple lines
        //multiple lines
        //multiple lines
        //multiple lines
      
    }else{
        
    //one line 
    but, if you did add an extra curly bracket at the very bottom, it would run fine.. but it's a waste of 2 characters if you're not doing anything else with that statement.
    so you only put a { if you plan to put a } at the end?
    PHP Code:
    if(statement){
        
    //multiple lines
        //multiple lines
        //multiple lines
        //multiple lines
      
    }else{
        
    //one line
      

    My usual boring signature: Something

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Recording incoming keywords from google searches.

    you only need to use curly brackets on a statement if it has multiple lines.

    examples:
    PHP Code:
    //this will run and do what is expected
    while($array mysql_fetch_array($query))
      
    print_r($array);

    //this will also run, but print_r($array) will only be fired once.
    //this will only execute print_r on the LAST value found from $query
    //because it is NOT a part of the while() statement.
    //$i will be equal to the number of results found
    while($array mysql_fetch_array($query))
      
    $i++
      
    print_r($array);

    //instead of the above, you would need to use the following for
    //expected results:
    while($array mysql_fetch_array($query)){
      
    $i++;
      
    print_r($array);
    }

    //you can also nest statements like so:
    if($variable == true)
      foreach(
    $array as $key => $value)
        echo 
    $key ' => ' $value "\n";

    //or, like this:
    if($variable == false)
      foreach(
    $array as $key => $value){
        
    $i++;
        echo 
    $key ' => ' $value;
      } 
    when you exclude the curly brackets, all you need to worry about is a terminating line (the terminating line is a valid-syntax statement that ends with a terminator (semi-colon -> (";"))). in the case of if() statements, you need to have a terminating line for each statement, like so (and, you can also add multi-lined statements at will by using curly brackets, also shown below):
    PHP Code:
    if($variable == true)
      echo 
    "variable is true";
    elseif(
    $variable == false && $variable2 == false)
      echo 
    "variable two is true, but variable is false";
    elseif(
    $variable == false && $variable2 == true){
      
    //this is a multi-line statement
      
    echo "variable two is true, but variable and variable three are false";
      
    $variable3 false;
    }else
      echo 
    "blah blah"
    Last edited by kows; Jan 27th, 2007 at 07:54 PM.

  9. #9
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Recording incoming keywords from google searches.

    While it is acceptable to omit curly brackets for a single statement construct, it is always a good idea to put them in as it improves the readability of the code and decreases the chances of introducing parse errors should another line be added.

    As for as PHP is concerned it is not a waste as prior to parsing all white space is stripped from the source code.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Recording incoming keywords from google searches.

    As for as PHP is concerned it is not a waste as prior to parsing all white space is stripped from the source code.
    Conceptually true, if technically incorrect.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Recording incoming keywords from google searches.

    Quote Originally Posted by CornedBee
    Conceptually true, if technically incorrect.
    What happens? Is it just ignored? The interpreter changes the source code into a type of byte code. This is what is executed.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Recording incoming keywords from google searches.

    Reading source code is typically a two-stage process. First comes the scanner, which converts the text into a series of tokens. A token is the smallest syntactic piece of a language. It can be annotated. For example, this code:
    Code:
    if($i == 10) { echo "Hello"; }
    would get scanned into this token stream (single characters are often represented by the character code):
    IF >> '(' >> VAR("i") >> EQ >> NUM(10) >> ')' >> '{' >> ECHO >> STRING("Hello") >> ';' >> '}'[/code]
    The second step is the parser, which knows the syntactic tree structure of the language and fits the tokens it gets into the tree. The result is the parse tree. It would perhaps parse the tokens into this:
    Code:
         block_if
       expr         block
      eq_expr  '{'   statements   '}'
    expr EQ expr        statement
    VAR     NUM        echo_statement
                    ECHO    expr     ';'
                            STRING
    The next step would be to convert this to an abstract syntax tree, but that's already beside the point. What is important is that the scanner simply doesn't include whitespace in the tokens it sends. So ignoring whitespace is part of the process, and not a preprocessing step. (That would be extremely inefficient.)

    PHP uses the T_ prefix for its token names, by the way. So if you ever encounter another "Unexpected T_STRING.", you know where it comes from. It's the parser parsing, for example, its echo_statement, and after the expression that is output, it expects a semicolon, but the scanner gives it a T_STRING.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Recording incoming keywords from google searches.

    Thinking of it logically that is what would happen.

    Point being - use as much white space as you like the only effect will be visually and the file size.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width