Jump to content

include_once causes "headers already sent by" for setcookie


Recommended Posts


I have strange problem that I did not have before with my host.


I have this php script

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

include_once "./incltest.php";
setcookie('Session', 'val23', 1577840461, '/');



And  the contents of incltest.php is:



The cookie does not set and the message i get it:


Warning: Cannot modify header information - headers already sent by (output started at /incltest.php:3) in test.php on line 7

I figured that if I remove closing tag ?> from php file that I include then *I no longer get this error. So far I've always used closing tags at the end of my php scripts and it has worked that ways. Are there any new php standards out there that now it doesn't work anymore? To me it looks strange to have starting tag <?php at the beginning of my file but not ending tag. Should I now change all my php files or would it be better to set some php ini value?

Link to comment
Share on other sites

If your script is purely php you do not require closing php tag.

The setcookie() must be set at top before any html or whitespace.

Your code has a empty line-break and then includes another file with empty line-break before setcookie() function result in headers already exist error.


Edited by dsonesuk
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...