Cookies time.

by 9 replies
11
I'm stumped.

I've tried 2 functions to try to do the same thing: Set a cookie that lasts for 30 days if the visitor is from Adwords. Both work partially, but have a big flaw.

The first one (below) will set the cookie and the cookie seems like it will be there for 30 days. The problem is that my site can't read the cookie on the first visit and it's important that it does that.

PHP Code:
function add_cookie() {
if (
$_GET['gclid']) {
$expire=time()+3888000;
setcookie("APT_cookie",$_GET['gclid'],$expire,"/",".mydomain.com");
}
}
add_action('send_headers','add_cookie'); 
The second one (below) will set the cookie and make the cookie available on the first visit. The problem with this cookie is that it disappears immediately after the page loads. So if I check the cookies right after the page loads, there's nothing there.

PHP Code:
function setcookielive() {
    
//set a cookie as usual, but ALSO add it to $_COOKIE so the current page load has access
    
$_COOKIE["APT_cookie"] = $_GET['gclid'];
    
$expire=time()+3888000;
    return 
setcookie("APT_cookie",$_COOKIE["APT_cookie"],$expire,"/",".mydomain.com");


add_action('send_headers','setcookielive'); 
When I try both functions together, the result is exactly the same as if I only use the second code snippet.

Does anyone have any idea what's going on here? I know why the first code snippet doesn't make the cookie available on the initial page load, but I have no idea why the second code snippet creates a cookie that disappears instantly!

BTW, using this in a Wordpress plugin.

Any help appreciated!
#programming #cookies #time
  • Perhaps a workaround is to store the value in a SESSION variable on the first load, then whenever your request the value look for if COOKIE else SESSION.

    Also, by my calculation there are 2592000 seconds in 30 days, so whatever you do expiry ought to be time()+2592000 (I think your number would be 45 days)

    -Ryan
    • [1] reply
    • Does it work if you remove the return as shown below:

      function setcookielive() {
      //set a cookie as usual, but ALSO add it to $_COOKIE so the current page load has access
      $_COOKIE["APT_cookie"] = $_GET['gclid'];
      $expire=time()+2592000;
      setcookie("APT_cookie",$_COOKIE["APT_cookie"],$expire,"/",".mydomain.com");
      }

      add_action('send_headers','setcookielive');
  • I recommend using jQuery's Cookie plugin: Plugins | jQuery Plugins
    • [1] reply
    • Does that jquery plugin allow the cookie to be accessed on the first page load?

      The reason I'm asking is because i'm going to have to learn how to use jquery to implement that solution and I'd like to avoid spending that time if it's still not going to resolve the issue!
  • You can't set and get a cookie in the same header (same page load). Your second example isn't working because you're not actually setting the cookie. Try removing the return.

    Also in the second example, you're not actually reading a set cookie - you're storing the value in the $_COOKIE global variable, then commanding the browser to actually set the cookie. On subsequent page loads, the cookie will be there when you set it properly.
    • [1] reply
  • Yes - you already have the cookie value, why do you need to set and read? Set your cookie, then continue your session.

    ronperkins' example is all you need.
    • [2] replies
    • Ur Both coding r perfact, the problem of with ur 30 days time, u take 45 days in expire fn, instead for i suggest u 2592000 seconds in 30 days

      Old Code
      $expire=time()+3888000;

      Change above code to new


      New code
      $expire=time()+2592000;



      Enjoy!!!
    • Finally figured it out:

      I removed the "return" as suggested.

      But I also had to put both my setcookie() function and my $_COOKIE variable assign inside an if statement.

      Basically I had to say: Only change the value of $_COOKIE and run setcookie() if the cookie is not already set.

      Thanks everyone for the help. Much appreciated!

Next Topics on Trending Feed

  • 11

    I'm stumped. I've tried 2 functions to try to do the same thing: Set a cookie that lasts for 30 days if the visitor is from Adwords. Both work partially, but have a big flaw.