在我的导航栏中,我的登录/注销php代码出现了一些问题。当用户登录后,我希望导航栏显示“注销”,反之亦然。
在header.php页面顶部:
<?php
session_start();
require "includes/dbh.inc.php";
?>
header.php中导航条的代码片段进一步向下:
<nav class="navbar navbar-expand-lg">
<div class="container-fluid">
<a class="navbar-brand text-left" href="index.php">iStudy University <i class="fas fa-graduation-cap"></i></a>
<button type="button" class="navbar-dark navbar-toggler" data-toggle="collapse" data-target="#myTogglerNav"
aria-controls="myTogglerNav" aria-label="Toggle Navigation"><span class="navbar-toggler-icon"></span>
</button> <!-- Aria controls are for assistive screen readers -->
<section class="collapse navbar-collapse" id="myTogglerNav">
<div class="navbar-nav ml-auto">
<a class="nav-item nav-link px-3" href="index.php"><i class="fas fa-home"><span class="nav-text"> Home</span></i></a>
<a class="nav-item nav-link px-3" href="about.html"><i class="fas fa-info-circle"><span class="nav-text"> About</span></i></a>
<a class="nav-item nav-link px-3" href="contact.html"><i class="fas fa-envelope"><span class="nav-text"> Contact Us</span></i></a>
<a class="nav-item nav-link px-3" href="signup.php"><i class="fas fa-check-square"><span class="nav-text"> Sign Up</span></i></a>
<?php
if (!isset($_SESSION['id'])) {
echo "<a class='nav-item nav-link px-3' action='includes/login.inc.php' href='login.php'><i class='fas fa-sign-in-alt'><span class='nav-text'> Login</span></i></a>";
} else {
echo "<a class='nav-item nav-link px-3' action='includes/logout.inc.php' href='index.php'><i class='fas fa-sign-out-alt'><span class='nav-text'> Logout</span></i></a>";
}
?>
</div>
</section>
</div>
</nav>
但是,当我进入index.php时,好像用户已经登录一样出现“注销”,请有人可以给我一些有关错误位置的指导。
**请注意,我不完全确定是否可以像我一样在代码中进行操作。...但是,我遵循的是YouTube教程,这些页面包含很多包含php的代码和mySQLi输入。**
action='includes/logout.inc.php
action='includes/login.inc.php
答案 0 :(得分:1)
不使用
$_SESSION['id']
检查用户登录。每个会话在启动时都会获得一个ID。而是设置一个会话变量,例如
$ _ SESSION ['user_id']
或某物。当用户登录并检查该变量时。