0

I have a login page when clicked on defines the variable $_session[username] = $name; this $_session['username'] is displayed on my main page and it works fine if someone does login. however when someone does not login, the error notice:undefined index pops up. if i use

<?php session_start(); if($_SESSION['username']==""|| isset($_SESSION['username'])){echo "Login";}else{echo "Logout";}?>

i thought it would fix the problem. but it has the same error

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

1 Answers1

0

if $_SESSION['username] is not set, you'll receive an undefined error message.

I'd recommend using empty(). It's the same thing as saying if( isset($_SESSION['username']) && $_SESSION['username] != '' ). This is more resource intensive (minimally in your case), as it has to check if the variable isset and that it isn't empty (blank, false, 0, null, etc).

if( empty($_SESSION['username']) ){
    echo 'Login';
}else{
    echo 'Logout';
}
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
  • i have gone through a couple of other posts and saw this too. however it did not fix the issue. the same error pops up again saying undefined index: username – Noiderate ana Feb 12 '20 at 15:26