"Undefined variable: _SESSION" VS. "The session has already begun."

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

Note: the session is already running ...

This is logical. But when I delete it, I get an error / notification message:

Note: Undefined variable: _SESSION

Why? And how do I fix this? Scripts work fine when I have session_start () two places in the script (just notice a little), but it doesn't work at all when I don't have two session_start ().

The only solution that has

error_reporting(E_ALL ^ E_NOTICE);

in my script? And is this bad practice just ignoring notifications?

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'];
        }

Conclusion:

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

: Undefined: _SESSION D:.....\main.php 21

NULL

pre_dump:

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

. session_start() - script, . .

+4

, unset($_SESSION); .

0

. $_SESSION.

1) $_SESSION session_start();. header.php. $_SESSION include.

<?php
 $username = $_SESSION['username'];
 //do some logical operation!!!
?> 
<?php include("header.php");?>

, -

<?php include("header.php");?>
<?php
 $username = $_SESSION['username'];
 //do some logical operation!!!
?> 

2) , , , , $_SESSION,

session_start();

Hope this helps anyone who stumbles upon the same issue. Although this happens in the late hours.

0
source

$ _ SESSION always exists whether you run session_start () or not. Therefore, I think something else should be causing your error.

Could you send the code? You might have disabled a variable somewhere (see this link ).

-1
source

Source: https://habr.com/ru/post/1779248/


All Articles