0

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?

8
  • 1
    session_start(); have you put this on the top just after <?php Commented Jun 26, 2014 at 14:46
  • Check your apache (or whatever webserver) error logs, maybe you're having an error you can't see with display_errors? Commented Jun 26, 2014 at 14:47
  • I am using session_start(), its in another <?php tag right at the top @MehulMohan Commented Jun 26, 2014 at 14:47
  • Try adding an isset() to your if test. Commented Jun 26, 2014 at 14:48
  • Change your else line to: <?php else:?> to troubleshoot. Looks like loggedin is neither false nor true. Commented Jun 26, 2014 at 14:49

6 Answers 6

2

If you tail your server logs (e.g. 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';
}
Sign up to request clarification or add additional context in comments.

3 Comments

What happens if there is session info that should persist despite login state? Not saying there is in this case but there could be.
I have sites which require that - in that case I name space everything inside the session e.g. $_SESSION['user']['id'], $_SESSION['user']['name'] etc... Then during logout you can just call unset($_SESSION['user']) and check using if (isset($_SESSION['user']))
Correct, I was just putting out a word of caution. In this case they could just use unset($_SESSION['loggedin']);
2

Try this:

<?php if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true): ?>
<h1>Welcome</h1>
<?php else:?>
<h1>Sign Up</h1>
<?php endif;?>

1 Comment

I agree. Geting rid of the elseif statement will help, plus the isset() check.
1

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;?>

Comments

1

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;?>

Comments

0

Try this way:

<?php if(!$_SESSION['loggedin']) { ?>
<h1>Sign Up</h1>
<?php } elseif($_SESSION['loggedin']) { ?>
<h1>Welcome</h1>
<?php } ?>

Comments

0

it is not resulting due to Undefined variable: _SESSION error. so you can utilize any ignorant (@ or &) Ex. @$_SESSION['loggedin'] OR &$_SESSION['loggedin']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.