Tag Archives: session_start(): Cannot start session when headers already sent in

Session_start(): Cannot start session when headers already sent in [How to Solve]

Open the php.in configuration file and rewrite its contents (it is not recommended to operate this file in Notepad here)

ctrl + f enter session.auto_start and find the position shown in the figure below

 

Modify its value

 

Pay attention to save and restart the Apache service, it has been successfully displayed

However, this is not the end, it reported a warning—-> The conversation has been started-Ignore—-> It looks like a symptom but not a permanent cure, then we continue to eliminate it

 

This warning is due to the fact that some users have already entered the session, and repeating session_start(); will cause the request to be repeated

In order to avoid more problems in the future, add a judgment to session_id, as follows:

// If session_id does not exist —- no session is stored, then open the session. If session_id exists, skip and do not execute session_start();

if (!session_id()) session_start();
At this point, no error or warning, green pass

 

Extension:
session_id() can be used to get/set the current session ID

The return value of session_id(): Returns the current session ID. If there is no current session, an empty string (“”) is returned

In the php.ini configuration, session.auto_start=0 (the session is closed by default), if you want to open the session, you need to call session_start() in the code actively;

Setting session.auto_start=1 in php.ini will automatically complete session_start(); —-> Executing session_start(); will generate a new session_id

The advantage of opening a session with session.auto_start=1 is that it will not cause errors because of forgetting to call session_start();. But we must pay attention to one thing: when using third-party code, be sure to delete all session_start();, otherwise the correct result will not be obtained