Using functions like header() and setCookie() after output in a website

636 Views Asked by At

I am developing a website with a login system. If the user logs in successfully, a cookie is set. The code looks a bit like that:

if(!success)
    echo "denied";
else
    setCookie(x,x,x,x,x);

Now, the problem is that code like this wouldn't work anywhere else than on a local test server (localhost). Now, my question is, are there any standardized ways to work around this? Or does the developer have to write his code so that problems like these don't appear in the first place?

1

There are 1 best solutions below

3
On

You can use use ob_start and ob_end_flush to activate the output buffering for specific code:

ob_start()
< code >
ob_end_flush()

or you can activate the output buffering for all your pages from the php.ini:

output_buffering = 'On'