Why a mistake?

setcookie("auto", "yes", time()+9999999); setcookie("email", "email123", time()+9999999); setcookie("password", "password123", time()+9999999); setcookie("id", "id123", time()+9999999); setcookie ("TestCookie","TestCookie"); 

This code gives errors

Warning: Cannot modify header information - headers already sent by (output started at Z:\home\qb1.cam\www\Test.php:4) in Z:\home\qb1.cam\www\Test.php on line 10

Warning: Cannot modify header information - headers already sent by (output started at Z:\home\qb1.cam\www\Test.php:4) in Z:\home\qb1.cam\www\Test.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at Z:\home\qb1.cam\www\Test.php:4) in Z:\home\qb1.cam\www\Test.php on line 12

Warning: Cannot modify header information - headers already sent by (output started at Z:\home\qb1.cam\www\Test.php:4) in Z:\home\qb1.cam\www\Test.php on line 13

Warning: Cannot modify header information - headers already sent by (output started at Z:\home\qb1.cam\www\Test.php:4) in Z:\home\qb1.cam\www\Test.php on line 16

Why

  • one
    Because the headers cannot be set after the body of the page is displayed, if the buffering of ob_ is turned off. First you make the header and the cookie, then the body of the page. Or use buffering. - ReinRaus
  • Is it possible to give an example or what ... Ato, I did not understand anything :( - Kirpich643
  • He says that these lines with setcookie should be at the beginning, before the content is displayed. - cranx
  • In general, it is always better to use buffering, if it is not a template. ps instead of echo, use a string variable and output it by echo at the end of the entire logic, and after setting the headers. - BiMaWa
  • Asli to this file is accessed using asynchronous connection with another stronysia is it not scary? - Kirpich643

1 answer 1

Before setting the header header, cookies or sessions do not need to display anything. Look in your code, have any output before setcookie. And it happens that the php file is closed <? ...?> and after it a line break costs, it is also considered as an output, so it is better to open the php file <? php and not close at all, in order to avoid accidental output.

  • And even if the file is in utf with BOM, then the BOM php will read the data for output to the browser. - ReinRaus