views:

51

answers:

2

Hi,

I'm setting a cookie on an anchor click on my page:

$("#btn_twitter_signin").click(function() {
    $.cookie("bookmarklet_twitter_signin", "true");
});

and on the server side i'm trying to retrieve it

$_COOKIE['bookmarklet_twitter_signin']

but strangely i'm getting an "Undefined index: bookmarklet_twitter_signin", though i made sure the cookie is saved correctly by checking from Firefox View Page Info, am i missing something?

Thanks.

Yehia A.Salam

+1  A: 

It may have to do with the path, try:

$("#btn_twitter_signin").click(function() {
  $.cookie("bookmarklet_twitter_signin", "true", {path: '/'});
});
Ben Rowe
Agreed. It could also have to do with the domain you are using when the cookie is set, and then the domain you are using to access the subsequent PHP script. You could specify your domain also (domain: '.domain.com').
TomWilsonFL
works like a charm
Yehia A.Salam
+1  A: 

The PHP won't be able to retrieve the set cookie until a page reload. The PHP, since it's server side, only runs once when the page is loaded. So if you change a cookie after a page load, your PHP has already run.

This means you need a page reload for PHP to see cookies set by Javascript.

You'd have to use the AJAX architecture, which Jquery makes easy, to be able to do this without a page refresh.

Use var_dump($_COOKIE) (like Itay said) or print_r($_COOKIE) to print all the cookies with PHP. You'll be able to see what's going on.

Peter Ajtai
Just a note that cookies are ALL client-side and sent with each request, so if the cookie is properly set with JS then it should be sent with any subsequent HTTP requests.
TomWilsonFL
I guess my explanation wasn't very good. Hope I clarified it.
Peter Ajtai