Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    84
    Thanks
    47
    Thanked 0 Times in 0 Posts

    Newbie needs help with Cookies please

    I'm new to php and thought this would be a good place to maybe get some answers.

    I track information by including it into the url. For example: www.mysite.com/index.php?tr=keyword [tr meaning tracking, and I am tracking what keyword a user searched for before they came to my site (I have a separate url for each keyword)]. When a user leaves my site through one of my advertising partners links on my site i use this format to extract the tracking information from the url: www.link.com?tracking=<?php echo $_GET['tr'] ;?>

    So basically I can determine where the sale originated from. My problem is that this only works when the user enters my site through the url with the tracking contained in it, and then leaves through one of my partners links.

    If the user enters my site through the link with the tracking information in it and then "shops around" on my site, then the tracking information is lost because subsequent pages they would surf will not have the "tr" included in the url.

    I'm guessing I have to somehow create a cookie that once the user enters my site, a cookie is set to the value of "tr" and then it can be called upon in any link throughout the entire site during that session.

    I would really appreciate if someone could maybe give me some code and tell me exactly where to place it. I have tried to play around with the code given in the php manual for setting cookies, but it is really hard for me to understand and I keep getting errors. Thanks in advance!

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Look into using php sessions. They are better than cookies as they stored on the server and not the user's system.

    http://www.tizag.com/phpT/phpsessions.php
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Sep 2006
    Posts
    84
    Thanks
    47
    Thanked 0 Times in 0 Posts
    Thank you soooooo much! That did it. I put this code in my main template as the first lines:

    <?php
    session_start();
    if(isset($_SESSION['tracking']))
    $_SESSION['tracking'] = $_SESSION['tracking'];
    else
    $_SESSION['tracking'] = $_GET['tr'];
    ?>

    and then I made my links look like: www.outgoinglink.com?id=?<? php echo $_SESSION['tracking'] ;?>

    Seems to work, and it is great that it was more simple than I thought. Thank you for allowing me to keep my sanity.

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You can probably just use this instead.
    PHP Code:
    <?php
    session_start
    ();
    if(isset(
    $_GET['tr']))
    $_SESSION['tracking'] = $_GET['tr'];
    ?>
    That will set the session only if the tr variable is set. The way you were doing it was a little redundant. If the variable isn't set then nothing will occur. The session will be updated as well if they happen to go to another url with the tr variable set.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    New Coder
    Join Date
    Sep 2006
    Posts
    84
    Thanks
    47
    Thanked 0 Times in 0 Posts
    I had that at first, but I think it was a problem because the first page they visit is the only page with the 'tr' variable set, if they go to another page, the code looks for 'tr' (I have the session code on every page) and can't find it. This was my problem in the first place, but if they travel to the second and subsequent pages, then 'tr' is set on the first page and I can keep setting it to itself until the user ultimately clicks on a link that calls for it. If you see a problem with my logic, by all mean let me know.

    Thanks again!

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I think the solution I posted would work because you are only wanting to set the session if tr is set anyways so I don't see the issue. If its not set on the other pages then no session is created. If it is set then the session is created with the most recent value. You don't have to keep setting it to itself because once set it is persistent. It will carry over to the end.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

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