简体   繁体   中英

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?

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.

It probably isn't working "on server," but is instead just not showing the error message to the page.

You can fix the warning re: the index by changing your if statement to this:

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

Or to whatever condition you need it to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM