仅在使用Bootstrap时无法修改标头

Alright, I was not able to find a problem like mine anywhere else, so here it is.

I am using a login function to login a user. After they put their data in, I send it through the function. This then returns values based on the result, which is used by a switch to display results to the user. If they get logged in, a cookie is set in the function. The switch then uses header('Location: /') to redirect the user.

The problem with this is that when I try using Bootstrap, it throws this error:

Warning: Cannot modify header information - headers already sent by
(output started at C:\wamp\www\assets\php\header.php:89) in
C:\wamp\www\assets\classes\Shortener.php on line 163

header.php; line 89: <br/><br/><br/> (this is the end of the file)

Shortener.php (login function); line 163:

setcookie('session', $db_session_hash, time()+3600, "/");


If I remove the bootstrap parts of this, it works absolutely fine! (which angers me the most)

header.php: https://pastebin.com/jnGtHSUy

Shortener.php (login function only): https://pastebin.com/i6CG54hw

I hope this is enough information. I've been angry at this ever since I've been getting this error.

Somehow your login function is being called partway through the page, instead of before the header file is included. There are 2 ways to solve this, Zak:

  1. Set the cookie first (by making sure that login() is always called before the template data), before you include any HTML. Shortly after you begin to output the page content (header content in this case), the server will output the HTTP headers (where the cookie should be), and then the HTML content. As soon as the server sends the headers, you can no longer set cookies.

  2. You can increase the output buffering as set in your php.ini file. I think I have mine set at 4096, but higher values mean more HTML content before the headers are sent. This is why the Bootstrap lines are the difference between overrunning the output buffer before you set your cookie or not overrunning the buffer's space. Now, this is a lousy solution albeit a handy piece of knowledge.

You need to set a larger output buffer in your php.ini or set it by .htaccess:

output_buffering = 12288

The default of 4096 may fall short once you add larger scripts and css sections.