Maintain Query Strings around my site

Discussion in 'PHP' started by dazfree, Jul 23, 2009.

  1. #1
    Hi,

    Quick simple question for you PHP geeks :)

    When someone visits one of my sites they usually have a query string attached to the domain (e.g. www.examplesite.co.uk/?kw=keyword) - however when they go to another page on my site the query string does not carry over.

    I know the solution is quite simple - do I just have to insert some PHP code at the top of my other pages to continue the session/query string? And also edit the links to the other internal pages?

    I really need to learn the basics of PHP...

    Any help much appreciated...

    Thanks,

    D
     
    dazfree, Jul 23, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    The best bet would be to set a session variable based on the kw in the url. Put this code at the top of every page, hopefully in the header file.

    Something like:

    
    
    session_start();
    
    if($_GET['kw'])
    {
    
    $_SESSION['kw'] = $_GET['kw'];
    
    } 
    
    //THEN ACCESS THE VARIABLE VIA $_SESSION['kw'];
    
    
    
    PHP:
    You will want to sanitize the $_SESSION['kw'] before using it in a database or other query to protect for an injection attack.
     
    jestep, Jul 23, 2009 IP
  3. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    As per jestep's suggestion make sure however to check your $_GET , since if you simply try to do if($_GET...) , or try to assign a variable that way, you'll get an error if kw was not passed yet.

    So something like this.
    
    session_start();
    
    if(isset($_GET['kw'])
    {
      $kw = $_GET['kw'];
      $_SESSION['kw'] = $kw;
    }
    else
    {
       if(isset($_SESSION['kw'])
       {
           $kw = $_SESSION['kw'];
       }
       else
       { 
           $kw = "";
       }
    }
    
    PHP:
    That way if a kw was passed via get , it'll save that on the page as $kw but also assign a session. IF not set, it'll check to see if it had already been saved in the session, if not, it sets it as blank (so you can act appropriately later) And you use isset to make sure its set so you don't end up getting a key error in PHP.

    The code above is obviously setup so that a $_GET in the querystring will override an existing session variable. IF you wish to go the other way around, check for $_GET on the outside, and $_SESSION on the inside (when isset is false for the get). This way once set on a specific page, it sticks until you destroy the session (such as with a logoff button).

    Also to sanitize before SQL usage, mysql_real_escape_string() works pretty good, just make sure you know when you go pulling it back from the database you may need to use stripslashes() (another method may need to be used if you're needing to do precise string comparisons in a SQL statement).
     
    kblessinggr, Jul 23, 2009 IP
  4. dazfree

    dazfree Active Member

    Messages:
    154
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Thanks for the replies guys. I'm totally a PHP novice, so really need to know i've got this right.

    adwords destination url:

    http://www.examplesite.co.uk?kw=123testing

    at the top of this landing page is:

    <?php
    session_start();
    $_SESSION['track'] = $_GET['kw'];
    ?>

    and in the affiliate link the search query is passed into the link as EPI:

    "http://www.affiliatelink.com/xxx/xxx/click?m=1111&a=11111&g=17557886epi=" . $_SESSION['track'];

    This works fine when a user stays on that page but if they visit another page on the site the ?kw=123testing no longer appears in the url and isnt passed into the affiliate link. My question is:

    * What do I need to put at the top of the new pages to pass the query string?

    * What other code do I need to put to make sure this works?

    Sorry about the stupid questions guys I really learn the basics to PHP.

    Thanks,

    D
    [/LIST]
     
    dazfree, Jul 23, 2009 IP
  5. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    as jestep previously mentioned you need that bit of code at the top of every page that will need the session variable (via a header, or so forth). Because without session_start(); $_SESSION['track'] never gets populated.

    Also I would strongly recommend using the code I provided instead of the single line code you have, since if kw= doesn't get passed you'll end up with a PHP error, likewise if you try to check for a session variable and the session hasn't been started nor assigned.

    Also the code I provided will check for the query string, and if does not exist will fall back to the session variable if saved (meaning it won't be in the url but it WILL get passed to the affiliate link). If you have $_SESSION['track'] = $_GET['kw']; at the top of every page, what happens when you goto the second page is your session then becomes blank, because $_GET['kw'] isn't set, which defeats the purpose of a session if you have to keep it in a url all the time.
     
    kblessinggr, Jul 23, 2009 IP