php - 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; ?>
解决方案
假设$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; ?>
推荐阅读
- javascript - Javascript - 对象属性设置的重复
- r - 使用 dplyr 包过滤
- css - ::first-letter 与 :first-child 有何不同(伪类与伪元素)
- javascript - 页面重新加载后 Nuxt 动态路由返回 404
- windows-10 - 从命令行启动“将文件类型或协议与程序关联”应用程序
- ios - 将 UIViews 放在前面
- bash - 用于更新最后一个字符的 Bash 脚本
- azure-active-directory - 在 Azure AD B2C 中刷新令牌吊销
- reactjs - 在 react-bootstrap@1.0.0-beta.5 中渲染 Nav.Link 时出错
- java - java: super 必须是构造函数中的第一个