So I'm checking if a user is logged in, and if so I'll remove the signup forms. So I thought I'd do it with if else statements. So here's my code
<?php if($_SESSION['loggedin'] === false): ?>
<h1>Sign Up</h1>
<?php elseif($_SESSION['loggedin'] === true):?>
<h1>Welcome</h1>
<?php endif;?>
For some reason all I get is a blank page. I'm using
error_reporting(E_ALL);
ini_set('display_errors', '1');
To display errors, but I'm not getting anything. On another page when doing a var_dump
on $_SESSION['loggedin'
I get bool(true)
, so I know that I'm logged in and expect Welcome
. I feel like its a syntax error. Any ideas?
Try this:
<?php if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true): ?>
<h1>Welcome</h1>
<?php else:?>
<h1>Sign Up</h1>
<?php endif;?>
If you tail your server logs (eg tail -F logs/php_error.log
) you'll probably see something like:
PHP Notice: Undefined variable: _SESSION
Try changing the code to:
if (isset( $_SESSION['loggedin'] ) && $_SESSION['loggedin'] == true )
{
echo 'Welcome';
} else {
echo 'Sign up';
}
Personally I'd also recommend changing your sign out logic to use unset($_SESSION)
that way you only have to check for:
if (isset( $_SESSION['loggedin'] ) ){
echo 'Welcome';
} else {
echo 'Sign up';
}
It's possible that neither code path gets executed if $_SESSION['loggedin']
isn't a boolean because the ===
comparison checks type and value. (See documentation for details)
A more safe and sane approach would be:
<?php if($_SESSION['loggedin'] === true): ?>
<h1>Welcome</h1>
<?php else:?>
<h1>Sign Up</h1>
<?php endif;?>
Try adding an isset()
to your if test.
<?php if(!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] === false): ?>
<h1>Sign Up</h1>
<?php else: ?>
<h1>Welcome</h1>
<?php endif;?>
Try this way:
<?php if(!$_SESSION['loggedin']) { ?>
<h1>Sign Up</h1>
<?php } elseif($_SESSION['loggedin']) { ?>
<h1>Welcome</h1>
<?php } ?>
it is not resulting due to Undefined variable: _SESSION
error. so you can utilize any ignorant (@ or &) Ex. @$_SESSION['loggedin']
OR &$_SESSION['loggedin']
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.