We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7966
    • 90 Posts
    I am not sure if this is modx related problem. I try to extract the search keywords via the following code I found.

    $url = parse_url($_SERVER['HTTP_REFERER']);
    $possibles_params = array(‘q’,'qt’,'p’);
    $params = array();
    parse_str($url['query'], $params);
    foreach($possibles_params as $val)
    {
    if($params[$val]) echo “referer : “.$url['host'].”keywords : “.$params[$val];
    }


    if I echo the $_SERVER[’HTTP_REFERER’], I see it is being processed.

    but it doesn’t parse the $url[’query’].

    if I copy and paste referrer into a paste_url and run the function like below, it works.

    $url = parse_url('http://www.google.com/search?hl=en&source=hp&biw=1040&bih=686&q=modx+best+cms&aq=f&aqi=&aql=&oq=&gs_rfai= ');
    $possibles_params = array(‘q’,'qt’,'p’);
    $params = array();
    parse_str($url['query'], $params);
    foreach($possibles_params as $val)
    {
    if($params[$val]) echo “referer : “.$url['host'].”keywords : “.$params[$val];
    }



    Anyone encountered this kind of problem with using server variables in snippets?


      • 7966
      • 90 Posts
      following solved the problem -

      htmlspecialchars_decode($_SERVER[’HTTP_REFERER’])
        • 25124
        • 54 Posts
        Hi dragona,

        I’m trying to get the terms that people use to find my site and store these so that the can be submitted as a hidden field with a feedback form. I’m not a great programmer and any assistance would be great in helping me do this.

        Regards
          • 7966
          • 90 Posts
          Hi SammyR,

          I don’t also have much experience neither with modx nor php, but here is what I did.

          I wanted to get the keyword and search engine where user comes from, and store that value in a cookie, and then use it to track clicks.

          Below is some code, maybe it might help.

          This one extract the keywords from major search engines. I found the extracting part from a website but can’t find the original address now.

          So this puts the values in place holder.

          <?php
            $thereferrer =  htmlspecialchars_decode($_SERVER['HTTP_REFERER']);
            $url = parse_url( $thereferrer );
            
          $possibles_params = array('q','qt','p');
          $params = array(); 
            parse_str($url['query'], $params);
          foreach($possibles_params as $val)
          {
              if($params[$val]) {
                  $searchengine = $url['host'];
                  $keyword = $params[$val] ;
              }
          }
          if ( isset($searchengine) && isset($keyword) ){
             $modx->setPlaceholder('sessionresult', array( "searchengine"=>$searchengine,"keyword"=>$keyword ) );
          }
          else {
           $modx->setPlaceholder('sessionresult', array( "searchengine"=>"other","keyword"=>"not found" ) );
          }
          



          There is another snippet which is called when a page is requested. "sp_SearchData_Get" is the previous snippet in this call. I tried to hold the values in a session, but it didn’t work. So keeping them in cookies meanwhile.


          if ( !isset($_COOKIE["se"]) && !isset($_COOKIE["kw"]) ) {
             $params=$modx->runSnippet("sp_SearchData_Get", $params);
             $searchvalues= $modx->getPlaceholder('sessionresult');
             setcookie("se", $searchvalues['searchengine'], time()+3600,'/',$_SERVER['HTTP_HOST']);
             setcookie("kw", $searchvalues['keyword'], time()+3600,'/',$_SERVER['HTTP_HOST']);
          }



          You can get the values back with following code and use it for filling a hidden field.
          $searchengine = $_COOKIE["se"];
          $keyword = $_COOKIE["kw"];
          


          hope it helps
            • 25124
            • 54 Posts
            Thanks Dragon for this. I’ll go through the code and see what I can try to get working.

            grin

            Regards
              • 27867
              • 2 Posts
              And did it work and how did you get it to work.

              I also need the keywords and referrer in a hidden field.

              Thanks in advance
                • 25124
                • 54 Posts
                Ended up using a php script that I dropped into the page. It isn’t possible for it to work as the referer is only the previous page.

                My solution was to;

                eForm writes to a database table 1 (inc IP address of submitter)
                every visit to a page is recorded to database table 2 (including referer)
                A php file accesses the database table 1 and lists all the forms submitted
                You can then click on IP address and it cross-references the search term in database table 2.

                A little convoluted, but works OK.

                Regards

                  • 7966
                  • 90 Posts
                  It isn’t possible for it to work as the referer is only the previous page.

                  Not sure why it doesn’t, but this usually work on my side if the browser passes the http_referrer value.

                  When a visitor comes from a search engine, it gets the search engine and keyword values, and set them to a cookie. So when visitor moves to another page (or many more pages) on your website, that second page do not try to get the referrer value if the cookies are already set for those.