Avatar ·

How to check if a PHP session is already started and avoid the "headers already sent" error

Hello everyone! I'm a beginner in PHP and I'm writing a small site with authentication. I have several scripts that work with sessions. In one file I start a session using session_start(), and then I include other files via include. The problem is that if I accidentally call session_start() twice in different files, PHP gives an error: Warning: session_start(): Cannot send session cookie - headers already sent I understand that this error occurs because headers have already been sent, and calling session_start() again is not possible. But how can I check if a session is already started so I don't call session_start() again? I tried using @ at the beginning to suppress the error, but I understand that's bad practice. Here's an example of my code: // config.php session_start(); $_