...

View Full Version : Newbie needs help with Cookies please



jdblewitt
07-19-2007, 01:57 AM
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!

_Aerospace_Eng_
07-19-2007, 02:38 AM
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

jdblewitt
07-19-2007, 03:36 AM
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.

_Aerospace_Eng_
07-19-2007, 07:33 AM
You can probably just use this instead.

<?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.

jdblewitt
07-20-2007, 03:48 AM
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!

_Aerospace_Eng_
07-20-2007, 04:36 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum