How can I set a cookie even after the headers have been sent? I was reading the PHP manual and it mentioned the ob_start() function as well as some others but I didn't understand them. Any ideas? I really don't want to have to move the code around as that would be a major pain in the rear end to do. Thanks!
Setting a cookie after headers have been sent
you don't unless output_buffering = On in php.ini
Ok well what can I do to solve the problem then? There has to be a way...
output_buffering = On in php.ini
There is no value set for the output_buffering on my server and I don't have access to the php.ini file so that rules that out. Is there any other way of doing this?
You cannot set a cookie after info has been sent.
However you could buffer your output as follows:
<?
ob_start();
// YOUR CODE HERE
// SET YOUR COOKIES HERE
ob_end_flush();
?>
That's what I was looking for however it didn't work. I tried it two ways: once right before I set the cookie (headers have already been sent) and once as the first line in the file, rest of the code, set the cookie, then flushed it. That didn't work either. Is there any special trick of where you need to place it? Thanks.
This is wrong:
<?
ob_start();
But this is right:
<?
ob_start();
Just make sure that ob_start() is on the first line.
Also, you will get an error if you use flush() before ob_end_flush(), since flush will output data to the browser immediately.
When I said flush I meant ob_end_flush(), sorry. Also, does this have to be on every page or just the login page?
Look at the example in the manual:
http://www.php.net/manual/en/ref.outcontrol.php
I'm guessing that you are outputting something before calling ob_start(); Probably some whitespace outside of the php tags in an include file.
I guess I'm not doing something right. Even with the ob_start() and the ob_end_flush() the cookie isn't being set. I've tested this by logging in and then closing the browser and going back to the page. It then asks me to login again.
rtfm!
here are some interesting links:
setcookie()
headers_sent()
$_COOKIE
If you are not getting an error, then your cookie is probably getting set. it may be expiring too quickly, or may have a different path than the function that checks it.
I guess I'm not understanding something then. The cookie time is set for 1 hour, so it shouldn't be expiring as soon as I re-open the window. I just checked my temporary internet files and I did not see the cookie anywhere in there. I'm sorry if I'm not understanding this.
so there is no cookie in your cache? that would seem to be a pretty reliable indication the cookies are not being set.
if i were you, i would start with a super simple test page and get it to set cookies reliably. THEN implement the ob_start() stuff and some echo statements. when you get that working, then think about bringing in your other code.
if you haven't read the documentation on the functions you are using, i would recommend that you do so.
persistence will make it happen.
and lastly, i only offer this very BAD solution because you seem frustrated. set a cookie with javascript. you could echo this at any point while your page is outputting and the cookie should get set via javascript when the script runs.
<script language="Javascript">
<!--
function writeCookie(name, value, hours)
{
var expire = "";
if(hours != null)
{
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
document.cookie = name + "=" + escape(value) + expire;
}
writeCookie("cookie_name", "my_value", some_number_of_hours);
//-->
</script>
oh...one more thing.
i'm not sure, but there is some limitation on setting a cookie and reading it on the same page. cookies for the page are loaded when you run the php script and i don't think $_COOKIE values change when you use setcookie().
RTFM!