How I resolved the PHP error related to session_start()

I had a PHP web application was giving an error. The error_log file on my web server showed:

[date] PHP Warning: session_start() [<a href=’function.session-start’>function.session-start</a>]: Cannot send session cookie — headers already sent in [myfile_path_here] on line 4[date] PHP Warning: session_start() [<a href=’function.session-start’>function.session-start</a>]: Cannot send session cache limiter — headers already sent in [myfile_path_here] on line 4

You see, in all of my scripts, I include a particular file as the very first line. In this file, I have session_start() as the very first thing. I couldn’t figure out what was causing this error.

I wrote a separate article with a sample PHP template that explains this setup.

It turned out that the cron job (an automatic script that runs at a specific interval, ex. every five minutes) was causing it. For an unknown reason, it didn’t like the session_start.

The solution was to create a duplicate “first configuration” file and simply remove the session_start() line.

It’s surprising though because there isn’t a browser that is calling this script, so I wouldn’t have thought that a session would have even started. 😕

Want to know more about how I recommend creating PHP web applications? If you already know what a “variable” is and the concept of an “if” statement, then check out my book: Web Development for Intermediate Programmers — with PHP

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store