Irina Farcau Irina Farcau - 4 months ago 19
PHP Question

login logout php setting variable

Any help over here?

<div id="menu">
<ul>
<?php
echo ''.(!isset($_SESSION['username']) ? '<li><a href="index.php?page=$pgname"><?=ucfirst($pgname)?></a></li>' : 'b');
$pgname="login";
//$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>

</ul>
</div>


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

Change your piece of code like below code,

<?php
$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