Jump to content

Problem with cookies - value disappears on page reload


rmelino

Recommended Posts

 

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.