I need a little help with php. I’m trying to include different menus for registered and unregistered users.
<?php
if ($_SESSION['user-class'] == 1) // 1 is class of registered users, which is assign after user log in
{echo '<a href="index.php?id=logout">Log out</a>'; }
else
{echo '<a href="index.php?id=login">Log in</a>'; }
?>
It works fine, but have one little problem: when the user is not logged in it gets a message
Notice: Undefined index: user-class
How to fix this?
You can instead use
if( isset( $_SESSION['user-class'] ) ), which will check whether the variable exists yet. Alternatively, you can just predefine the$_SESSION['user-class']variable for all users, logged in or not, in your header files.