tags:

views:

45

answers:

3

hello sir.. i just want to ask if do i need to set something to enable the cookies from my hosting?

i have this

<?php
 setcookie("TestCookie","Hello",time()+3600); 
 print_r($_COOKIE);
 ?>

it will function perfectly at my server which is xampp. but when i upload it to my hosting, it will not function.. what should i do? or what will i add to the code?

A: 

Cookies are sent as http response headers. Those headers can only be sent before anything from the response body has been sent.
Make sure no output happens before setcookie():

  • no echo, printf, readfile
  • nothing outside the <?php ?> tags, not a single white-space or BOM in any of the scripts that have been included and executed for the same request before setcookie()
  • increase the error_reporting level and check for warnings/notices. Those messages are usually logged in a file (e.g. error.log). You can set display_errors to true to see them in the output.

PHP can be configured to use output buffers. In this case output is not sent directly to the client but held in a buffer until either it's full, the buffer is flushed or the script ends (implicit flush). So until the content of the buffer is actually sent to the client you can set/modify/delete http headers like cookies. see http://docs.php.net/outcontrol.configuration

VolkerK
the code above contains no echo, printf, readfile.. but still not functioning sir..
vrynxzent
Did you increase the error_reporting level? Simplest, quickest (though arguably not the best) way to do this is to add `error_reporting(E_ALL); ini_set('display_errors', 1);` to the script, somewhere where it is executed prior to `setcookie()`.
VolkerK
i did it but there's no error going out..<?phpsetcookie("TestCookie","Hello",time()+33600);print_r($_COOKIE);echo date('e P');error_reporting(E_ALL); ini_set('display_errors', 1);?>
vrynxzent
"somewhere where it is executed **prior** to setcookie()" - `<?php error_reporting(E_ALL); ini_set('display_errors', 1); setcookie("TestCookie","Hello",time()+33600); ?>`
VolkerK
A: 

Use web developer toolbar to view cookie.

My problem days in practicing PHP reminds me:

  • there may be new line ahead of <?php [this occurred to me when I use some proxies to upload]
  • free hosting providers include("top_ads.php") that is put on top of your php file [this occurred to me when I used free hosting]
jack
when i put include("top_ads.php") on top the there's an error that failed to open stream: No such file or directory
vrynxzent
A: 

It is also possible that the cookie is actually sent but the client doesn't sent the value back to the webserver in subsequent requests.
Possible causes:

  • your server's clock is misconfigured and therefore time()+3600 is in the past from the client's perspective => the client will delete the cookie immediately.
  • the cookie failed the general tail match check, search for "tail match" in http://curl.haxx.se/rfc/cookie_spec.html
  • the client is configured not to accept those cookies

There are many addons for different browsers that let you see the http headers the client actually received. E.g. Firebug for Firefox. Use them to check if there is a Set-cookie header in the response. If there is the client for some reasons rejected it. If there is no such header you have to check why the server didn't sent it.

VolkerK
my server's clock is US/Central -05:00 is it okay?
vrynxzent
i got it sir.. thanks for your answers.. your server's clock is misconfigured and therefore time()+3600 is in the past from the client's perspective => the client will delete the cookie immediately. that's the answer to my question.. i only remove the time()+3600
vrynxzent