Search code examples
phpsessionlocalhostmamp

Session doesn't work on MAMP


When i try to run my site on localhost i get an error:

Undefined index: log in ... on line 137

Within this file there is a line:

if (!$_SESSION['log']) { ...

Everything works on server, but not on localhost. What can I do to fix it?


Solution

  • There is probably a difference between the level of error reporting between the server and your local setup.

    If you want to check if the variable is set (assuming that a session has been started...), you should use:

    if (!isset($_SESSION['log'])) {
    

    Or if you want to check if it is not set and / or empty or false:

    if (empty($_SESSION['log'])) {
    

    Both will not generate any warnings for non-set variables or array indices.