Warning: session_start(): Cannot start session when headers already sent - despite the function call is in top of the page

Written on Sunday May 10, 2020 - Permalink - Tags: linux, php, server, utf-8

Problem

I've got session_start() call in first line of the PHP script file but server throw me warning:

Warning: session_start(): Cannot start session when headers already sent in /path/to/file.php on line 2

Solution

Remove BOM (Byte order mark) from file.

In JetBrain's IDEs you can open context menu in editor area and select Remove BOM option. You can also create new UTF-8 file without BOM and copy content.