There is a file user.php where I work with the $ _SESSION variable. It is connected to index.php but the variable is not defined anywhere except user.php . What should be done so that the variable and its indices are visible in any of the three files?

<?php //index.php include "cfg.php"; //Тут лежит функция session_start(); include "user.php"; //Операции с $_SESSION ?> 

    1 answer 1

    This is a 'superglobal' or automatic global variable. This simply means that it is available in all script contexts. There is no need to execute the global $ variable; to access it inside a method or function.

    PHP: $ _ SESSION

    That is, $ _SESSION is available everywhere.

    • But I have problems with access to this variable from different files. I write something in $ _SESSION in one file, but I have access to actions with the necessary indices in another ... In the above code example, I don’t have access to the variable indices in index.php. What could be the reason? - Alexander Ryabikov
    • @AlexanderRyabikov tried to perform var_dump($_SESSION) in index.php? - Stanislav Grot
    • There were no any indexes - Alexander Ryabikov