Forum Moderators: coopster

Message Too Old, No Replies

cookies not working in the server

         

charlee

8:18 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



Is it possible that if just set several cookies with
setcookie('name', 'value') ;
and after I try to call them with

if (isset($_COOKIE['name']){do something;}

be wrong somehow, because it works on my computer but not in my server and Im wondering if Im doing something wrong or ambiguos here :S

edit:

by the way I was testing now the php code because before it was being called from a ajax based js file so I didnt know exactly what the responseText was holding. But now i executed just the php file and it sais that header were sent already when i try to set the cookie in line #xx so I guess this might be the problem but still dont get why it works on my computer and not on my server :( and still dont know how to fix this :(

thanks in advance.

PHP_Chimp

8:41 pm on Jun 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are setting the cookie with php then you need to set it as the first thing. As a cookie is set as part of the http headers that are sent with the page (hence your warning about headers already sent).

To get around this either re-position your setcookie statement, or use output buffering. It would be cleaner to just move your setcookie statement, as ob_buffering is a work around not a solution and can introduce its own problems.
You could also use javascript to set the cookie a a later point. With the problem that if javascript is disabled then the cookie will never get set.

If the setting of the cookie is working on one system but not on another then you need to have a look at what is different between the systems.
Are you using the javascript on your localhost, but php on the server?
Are you using javascript to set the cookie, but not allowing javascript from non-localhost's?
Are you setting the cookie domain? As if you are setting the domain as localhost then that is going to confuse your browser when getting a cookie from your server.

I assume that you mean
if (isset($_COOKIE['name']))// missing closing ) for if statement
{do something;}

;)

charlee

8:53 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



Thanks! the thing is that i had an echo right before setting my cookies this output was being taken from an asynchronous request and anything else so I just put it after my cookies as u just mentioned and it works! thanks god. I have a local server to test my php code so yes i was using only php for the cookies not js. But obviously i know the php.ini from my server and my local server are different so this might have been the reason why I was having this overhead.

You were mentioning something about js disable, actually if the user disables js they cant vote(its an ajax rating system) :( so they better have it enabled but anyways js is not setting cookies here.

thanks again

PHP_Chimp

6:18 pm on Jun 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad that you got it sorted.

Could you not use php to back up the ajax for those people that disable javascript?
As you should not need to change much of the code, as ajax is talking through php anyway.