-1

I am trying to make a library system, everything is going well, but I faced with such a error

Notice: Undefined index: user_log in C:\xampp\htdocs\e_library\top.php on line 23

and line 23 is here:

<?php 
$user_log = $_SESSION['user_log'];
if (isset($_SESSION['user_log'])){
    echo "<a href='#' style='color:#FFC'>Welcome $_SESSION[username] </a> ||  <a href='logout.php' style='color:#FFC'>Logout</a>";
}
else{
    echo "<a href='login.php' style='color:#FFC'>Sign In</a>";
}
?>
qwertynl
  • 3,912
  • 1
  • 21
  • 43
  • 4
    You're using `$_SESSION['user_log'];` before you check it exists. – andrewsi Dec 24 '13 at 17:34
  • [**YOU WIN A COOKIE**](http://static.tumblr.com/af06dd0807d256dc1dec8abe12cd8f1b/oghd6vg/FBSmgqgxn/tumblr_static_chocolate_chip_cookies.jpeg) Have fun with it @andrewsi ! – qwertynl Dec 24 '13 at 17:35

2 Answers2

1

You should put $user_log = $_SESSION['user_log']; inside your if (isset($_SESSION['user_log'])) block, instead of before.

Floris
  • 45,857
  • 6
  • 70
  • 122
Paul Lo
  • 6,032
  • 6
  • 31
  • 36
  • That would be my first response, too; but the `$user_log` variable isn't used in the snippet we can see. What I'd suggest would be to initialise it outside the `if` to blank, too - otherwise, you risk an `undefined variable` message from further down the code. – andrewsi Dec 24 '13 at 17:37
  • Also, in the if statement he has $_SESSION[username] and username should be in quotes. – samuraiseoul Dec 24 '13 at 17:38
  • Ya...the snippet has shown more than one issue – Paul Lo Dec 24 '13 at 17:39
0

Please check first:

if (isset($_SESSION['user_log'])){ // Because session array does not contain this yet.
        //welcome message and $user_log = ..
}else{

}
Riad
  • 3,822
  • 5
  • 28
  • 39
  • tnx, you mean , I should use nested if? – user3024597 Dec 24 '13 at 17:41
  • no, i think the previous answer is identical to mine. only the $user_log should be inside if block. :) Note: Actually $_SESSION array does not contains the index 'user_log', so it says an undefined index. – Riad Dec 24 '13 at 17:46