rmelino Posted January 4, 2011 Share Posted January 4, 2011 Hello, I am having issues with getting cookies to function how i'd like them to. My goal is to be able to capture a cookie value from the url if a user comes to my site via something like adwords. An example URL they could come to is http://www.mysite.com?kw=hello I am trying to capture the word 'hello' and tie it to that visitor regardless of where they go on the site. I need to use that value to pass through if they complete a form on my site so that i can track the keyword source. I have included the following as the first line of code on every page of my site: $kw = $_GET["kw"]; setcookie('kw',$kw); Then, if i go to this url: http://www.mysite.com?kw=hello and then later go to a url like this: http://www.mysite.com/page1.html, i am able to echo out the cookie on page1.html using the following code which is contained in page1.html: echo $_COOKIE["kw"]; The problem is, if i then refresh page1.html i can no longer echo out the cookie--it disappears. I need the cookie to stay with the user no matter what page they are on within the site and not matter how many different pages they visit. I hope i am explaining myself in a way someone can understand. Perhaps cookies aren't the best way? Should i use sessions instead? Thanks in advance for your help Quote Link to comment Share on other sites More sharing options...
inversesoft123 Posted January 4, 2011 Share Posted January 4, 2011 you must set expiration time for cookies which you are setting on users browser. eg: setcookie("KW", $kw, time()+60*60*24*60); For more regarding time visit http://php.net/manual/en/ref.datetime.php Quote Link to comment Share on other sites More sharing options...
revraz Posted January 4, 2011 Share Posted January 4, 2011 I would set a session instead of a cookie, unless you want it to remain the same if they return later directly Quote Link to comment Share on other sites More sharing options...
rmelino Posted January 4, 2011 Author Share Posted January 4, 2011 Thank you for the replies... I would like to stick with a cookie so that it stays the same if they return. Also, inversesoft123, i added the code you suggested so that it would not expire and I am still having the same issue. The cookie echo's out on page1.html when i visit it for the first time but if i reload the page it disappears. Any ideas as to why this is happening? Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted January 4, 2011 Share Posted January 4, 2011 Is the webserver set up to parse .html files as PHP? Quote Link to comment Share on other sites More sharing options...
rmelino Posted January 4, 2011 Author Share Posted January 4, 2011 I believe so, i have the following line in .htaccess AddHandler application/x-httpd-php .htm .html .xml All my other php code on the site is running fine Is this what you mean? Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted January 4, 2011 Share Posted January 4, 2011 Yes, I was just making sure. You are not setting the cookie in page1.html, are you? Because then it would set to be blank when you refresh without the ?kw=. Quote Link to comment Share on other sites More sharing options...
rmelino Posted January 4, 2011 Author Share Posted January 4, 2011 Actually now that you say that I think that is the issue. I am essentially setting the cookie on every page of the site through an include file. The include file which runs first on every page has this code: $kw = $_GET["kw"]; setcookie("KW", $kw, time()+60*60*24*60); So then is it possible to somehow set the cookie on every page but avoid having the cookie reset if one is already present? I'm guess this is done through isset and an if statement? Thanks again for your help Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted January 4, 2011 Share Posted January 4, 2011 It is exactly as you say. I usually prefer empty() over isset() for these things, though. If you wanted to set the cookie only if a ?kw= is actually supplied, you could do: if(!empty($_GET['kw'])) { setcookie('KW', $_GET['kw'], time()+60*60*24*60); } Or, if you wanted to make sure the cookie was only set once: if(empty($_COOKIE['KW'])) { setcookie('KW', $_GET['kw'], time()+60*60*24*60); } Edit: Made a few corrections. Quote Link to comment Share on other sites More sharing options...
rmelino Posted January 4, 2011 Author Share Posted January 4, 2011 That seems to be working--thank you! One last issue though. I am ultimately trying to pass this cookie into a php redirect page called goto-google.html that looks like this: <?php header("Location: http://www.google.com?q=".$_COOKIE['kw'].""); ?> For some reason the kw cookie is not getting passed into the Location: url line above. Am i doing something wrong with the syntax? Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted January 4, 2011 Share Posted January 4, 2011 Note that there is a difference between uppercase and lowercase letters. You are setting the cookie as 'KW', but accessing it as 'kw'. Edit: Oh, also. the last ."" can be removed as it is simply appending an empty string. Quote Link to comment Share on other sites More sharing options...
rmelino Posted January 4, 2011 Author Share Posted January 4, 2011 ahhh, that was it. Thanks again for all your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.