PHP Trick

Often, when there are multiple guys working in one big website, not all of them in the same time and with the same coding standard, you get some really weird stuff in the code.

For example, we’ve got a problem with session_start(). We had 3-4 files in which session_start() was the second line (after <?php of course). Being included in several other pages, we had, of course :

Notice: A session had already been started – ignoring session_start() in /home/*****/******.php on line *

After a couple of tries we have fixed the issue:

$sessionId = session_id();
if($sessionId == “”) {
session_start();
}

PS: Don’t try if(empty(session_id()) { because :

Fatal error: Can’t use function return value in write context

One comment

Leave a comment