Please help. I want to pass a variable across all the pages of my site.

Discussion in 'PHP' started by mike33pt, Jun 29, 2009.

  1. #1
    My Google adwords destination URL is: http://www.mydomain.com?kw={keyword} this will ensure that Google Adwords passes my keyword to my landing page as a variable "kw".

    Now where ever on my page I place <?php echo $kw;?> I can show these keywords on my page.

    All is cool so far.

    All I want is to maintain this variable across all the pages of my site.

    I could do it by passing it in the url but this is very messy.

    There is a way by adding some code to start of every page of my site. I have tried all sorts and can't get it to work.

    Could some explain in baby steps what I have to do please?

    I appreciate any help

    Thanks

    Mike
     
    mike33pt, Jun 29, 2009 IP
  2. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use a cookie:

    if (isset($_GET['kw']))
      {
      setcookie('keyword', $_GET['kw']);
      $kw = $_GET['kw'];
      }
    if (isset($_COOKIE['keyword']))
      {
      $kw = $_COOKIE['keyword'];
      }
    PHP:
     
    JDevereux, Jun 29, 2009 IP
  3. mike33pt

    mike33pt Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for that, but it did not work.

    I have the echo on every page but it only echoed on the page that I loaded.

    with ?kw=Testing at the end of the url it echos: "Testing" on page.

    It did not pass through to other pages.

    Thanks again, though.

    A cookie was set but it is not reading it??

    Regards

    Mike
     
    mike33pt, Jun 29, 2009 IP
  4. mike33pt

    mike33pt Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Think I'm getting there. There was some other code screwing that up.

    I'll post back soon, thanks.
     
    mike33pt, Jun 29, 2009 IP
  5. mike33pt

    mike33pt Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok I got it.

    In one place on one of my pages I need to display the keyword without any spaces.

    This code was doing this:

    <?php echo str_replace( ' ', '_', $_GET['kw'] );?>
    Code (markup):
    It was stripping the spaces and replacing them with "_" but for some reason it will not work with that cookie code in place.

    I have taken my:
    <?php echo str_replace( ' ', '_', $_GET['kw'] );?>
    Code (markup):
    out and it all works as it should now on every page. But in this particular place in one page I cannot have spaces in the keyword phrase.

    Any ideas?

    Thank you

    Regards

    Mike
     
    mike33pt, Jun 29, 2009 IP
  6. nishanair

    nishanair Active Member

    Messages:
    188
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Use Session, that may be better

    Are you using cookies, if you then why are you using the GET in the pages?
     
    nishanair, Jun 29, 2009 IP
  7. mike33pt

    mike33pt Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for your help. I have it working now. I did not need to strip the spaces after all.

    JDevereux's post fixed my problem, so big thank you.

    Regards

    Mike
     
    mike33pt, Jun 29, 2009 IP
  8. mike33pt

    mike33pt Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I need a little more help on this please...

    I checked my cookie after I tested it and it expires at the end of the session.

    How would I set to expire after say 30 days?

    Thanks

    Mike
     
    mike33pt, Jul 10, 2009 IP