Irina Farcau Irina Farcau - 1 year ago 67
PHP Question

login logout php setting variable

Any help over here?

<div id="menu">
echo ''.(!isset($_SESSION['username']) ? '<li><a href="index.php?page=$pgname"><?=ucfirst($pgname)?></a></li>' : 'b');
//$pg = "logout";
<li><a href="/">Home</a></li>
<li><a href="index.php?page=register">Register</a></li>
<li><a href="index.php?page=admin">Admin</a></li>
<li><a href="index.php?page=update">Update</a></li>
<li><a href="index.php?page=profile">Profile</a></li>
<li><a href="index.php?page=$pgname"><?=ucfirst($pgname)?></a></li>
<li><a href="index.php?page=$pgname"><?=ucfirst($pgname)?></a></li>


I want when the user is already logged in to print only the logout menu button, and otherwise when is logout, only the log in to be appeared on the menu. how can i achieve that?

Answer Source

Change your piece of code like below code,

$pagename =  !empty($_SESSION['username']) ? 'login' : 'logout';

Remove last two lines & change to this

<li><a href="index.php?page=<?php echo $pagename;?>"><?php echo ucfirst($pagename)?></a></li>

Note: dont forget to start session on very first line if your current code does not getting called by any other file