0

php version is:

PHP 7.2.24-0ubuntu0.18.04.7 (cli) (built: Oct  7 2020 15:24:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.24-0ubuntu0.18.04.7, Copyright (c) 1999-2018, by Zend Technologies

my code is :

error_reporting(E_ALL);
ini_set("display_errors", 1);
echo 'some random output ';
$adminCookie = "emFrYXRfZWxtX3JvYm90";
setcookie("admin", $adminCookie, time()+3600*3000, "/");

i was expecting an error because the cookie goes to header section and the data cannot be sent before it.

otherwise it would give an error like:

Warning: Cannot modify header information - headers already sent by (output started at /location/to/script.php) in /location/to/script.php on line x

but the code works fine and no errors being displayed! and the cookie is added to the browser after inspecting with chromes built-in tool. is ob_start() on by default on php 7.2.24-0ubuntu0.18.04.7 ?

  • 1
    Not sure, not reproducible https://3v4l.org/upZ1W except in older versions. Check your `php.ini` Try `var_dump(ini_get("output_buffering"));` – AbraCadaver Dec 31 '20 at 21:04
  • Not sure why this question was closed as duplicate, it does deal with output buffering but answering "what is output buffering?" does not explain why (and under what circumstances) this code works. – Filip Roséen - refp Dec 31 '20 at 21:44
  • exactly, and thanks for "Not sure why this question was closed as duplicate". back in when i used to code in php if you have any output before setcookie() it would generate an error but i think in the new versions of PHP and Apache they have changed it. – zakat_elm_robot Dec 31 '20 at 21:53
  • i do very politely ask for my question to be reopened because it leads to some new programmers to understand that a php function such as setcookie() Does behave different on different versions. i found that out thanks to AbraCadaver. also thank Filip Roséen - refp for his comment. – zakat_elm_robot Dec 31 '20 at 22:00
  • var_dump gives me this string(4) "4096" – zakat_elm_robot Dec 31 '20 at 22:05

0 Answers0