首页 > 解决方案 > PHP swap between login/logout when user login/logout

问题描述

Am trying to swap between login displaying username when they login (username is stored in the database) and logout when they sign out

here is my code :

    <?php if(isset($_SESSION['username'])): ?>
        <li><a href="logout.php">Logout</a></li>
         <?php echo $username; ?>
        <?php else: ?>
       <li><a href="login.php">Login</a></li>
 <?php endif; ?>

标签: php

解决方案


假设$username设置在某个地方,您需要使用<?php ?>标签来实际操作echo

<?php if(isset($_SESSION['username'])): ?>
  <li><a href="logout.php">Logout</a>
  <?php echo $username; ?> 
  </li>
<?php else: ?>
  <li><a href="login.php">Login</a></li>
<?php endif; ?>

如果$username未设置,请使用会话变量

<?php if(isset($_SESSION['username'])): ?>
  <li><a href="logout.php">Logout</a>
  <?php echo $_SESSION['username']; ?> 
  </li>
<?php else: ?>
  <li><a href="login.php">Login</a></li>
<?php endif; ?>

推荐阅读