我的登录表单可以正常工作,但是如果我单击注销按钮,则注销不成功,它进入了index.php,位置正确,但没有显示任何东西(空白)白屏
登录表格:index.php
<?php
require('CONFIG/config.php');
require('CONFIG/db.php');
if(empty($_SESSION)) // if the session not yet started
session_start();
if(isset($_SESSION['email'])) { // if already login
header("location: ../dashboard.php"); // send to home page
exit;
}
?>
<form class="form-horizontal" role="form" method="POST" action="PHP/action_login.php">
<div class="form-group">
<label class="col-md-4 control-label">Email</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-success col-md-6" name="submit" style="margin-right: 15px; background-color:#069370;">Login </button>
</div>
</div>
</form>
操作登录:PHP / action_login.php
<?php
session_start();
if(isset($_POST['submit'])) {
include('CONFIG/config.php');
include('CONFIG/db.php');
$Email = $_POST['email'];
$Password = $_POST['password'];
$_SESSION['email'] = '$Email';
//erroe handler
//check if the input is empty
if(empty($Email) || empty($Password)) {
header("Location: ../index.php?login=error");
exit();
}else{
$sql = "SELECT * from users where email='$Email' AND password='$Password'";
$sql2 = "SELECT roles.id from users, roles where users.email='$Email'";
$result2 = mysqli_query($conn, $sql2);
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck < 1){
header("Location: ../index.php?login=error");
exit();
}else{
$_SESSION['roleid'] = $result2;
header("Location: ../dashboard.php?login=success");
exit();
}
}
}else{
header("Location: ../index.php?login=error");
exit();
}
注销表单:dashboard.php
<?php
include 'connect_to_database.php'; //connect the connection page
if(empty($_SESSION)) // if the session not yet started
session_start();
if(!isset($_SESSION['email'])) { //if not yet logged in
header("Location: ../index.php");// send to login page
exit;
}
?>
<li class="sub-menu"><a href="#">Admin</a>
<ul>
<li><a href="users_management.php"><i class="fa fa-user"></i>
<span>Users Management</span></a></li>
<li>
<a href="PHP/action_logout.php">
<i class="fa fa-arrow-left"></i>
<span class="title">Logout</span>
</a></li>
</ul>
</li>
操作注销:action_logout.php(在PHP文件夹内)
<?php
session_start();
session_unset();
session_destroy();
header("Location: ../index.php");
exit;
?>
问题是当我单击注销时索引表什么也没显示,为什么?请检查我的代码,谢谢
答案 0 :(得分:2)
在action_logout.php中,您必须删除session_start()
,因为只需创建一个新会话然后销毁它
<?php
session_unset();
session_destroy();
header("Location: ../index.php");
exit;
?>
和index.php
您需要检查会话是否为空
通过
if(isset($_SESSION) && empty($_SESSION)) {
session_start()
}