Php – “Undefined variable: _SESSION” VS. “A session had already been started”

error-reportingphpsession

Hey.
I have some php scripts. In one of them i have the code session_start(), and when I in another script again have session_start() i get the notice:

Notice: A session had already been started…

Thats logical. But when I remove it I get the error/notice:

Notice: Undefined variable: _SESSION

Why? And how do I fix it? The scripts works fine when I have session_start() two places in the script (only get an little notice), but doesn't work at all when I doesn't have two session_start().

Is the only solution to have an

error_reporting(E_ALL ^ E_NOTICE);

in my script? And isn't that bad practice to just ignore notices?

Edit:

Parts of my code:

            try {
            //session_start();

            $STH = DB::prepare  (   "UPDATE users SET DJ_name=?, DJ_showname=? WHERE id=?" );
            $STH->execute(array($_POST['DJ_name'], $_POST['DJ_showname'], $_SESSION['userid']));

            pre_dump($_SESSION);

            $_SESSION['DJ_name']        =   $_POST['DJ_name'];
            $_SESSION['DJ_showname']    =   $_POST['DJ_showname'];
        }

Output:

Notice: Undefined variable: _SESSION in D:…..\main.php on line 19

Notice: Undefined variable: _SESSION in D:…..\main.php on line 21

NULL

pre_dump code:

    function pre_dump($var)
{
    echo '<pre>';
    var_dump($var);
    echo '</pre>';
}

Best Solution

I found the error myself. Had an session_start() somewhere in my script that I didn't notice. Everything seems working now.

Related Question