我有一个登录页面,该页面将检查您的用户和密码。如果正确,它将引导您进入您的用户页面,但是,当我两次输入用户名和密码时,我只会感到困惑。它不会立即设置$_SESSION['user'];
。为什么?
这是登录页面(它是一个单独的文件,从另一个文件中调用,该文件从index.php调用):
<?php
header('content-type: text/html; charset: utf-8');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<div id="login-form">
<?php
$rop = $DB->query("SELECT * FROM admin_users");
// var_dump($rop);
if (isset($_POST['log-in'])) {
if (empty($_POST['login']) || empty($_POST['password'])) {
header("location:index.php?empty=Fill in all the fields");
} else {
$login = smart($_POST['login']);
$pass = smart($_POST['password']);
$select = $DB->query("SELECT * FROM `admin_users` WHERE `user` = '$login'");
$user = $select->num_rows;
if ($user == 1) {
$_SESSION['user'] = $login;
$x = $select->fetch_assoc();
$passh = md5(md5($pass));
$selects = $DB->query("SELECT * FROM `admin_users` WHERE user = '$login' AND pass = '$passh'");
$users = $selects->num_rows;
if ($users == 1) {
$_SESSION['pass'] = $passh;
header('Refresh: 0; url=' . $base_url . 'user');
} else {
header("location:index.php?Invalid=Wrong username and/or password!");
}
} else {
header("location:index.php?wrong=Wrong username and/or password!");
}
}
}
?>
<form method="POST" name="login-form" onsubmit="return validateLogin()">
<?php
if (@$_GET['empty'] == true) {
?>
<center style='color: red;'><?php echo $_GET['empty'];?></center><br>
<?php
} elseif (@$_GET['wrong'] == true) {
?>
<center style='color: red;'><?php echo $_GET['wrong'];?></center><br>
<?php
}
?>
<p id="email-login" class="email">Email<span class="required">*</span><img src="ico/ic-mail.png" align="right" class="ic-mail"></p>
<input id="email-login1" type="text" name="login">
<p id="pwd-login" class="pwd">Password<span class="required">*</span><img src="ico/ic-lock.png" align="right" class="ic-lock"></p>
<input id="pwd-login1" type="password" name="password">
<a id="forgot" href="#" class="forgot">Forgot?</a>
<button id="log-in" type="submit" name="log-in" class="loginb"><span style="cursor:pointer">login</span></button>
</form>
</div>
一旦我提交了用户名和密码,如果它们正确的话,它将被定向到user / index.php其中:
<?php
session_start();
include_once('../inc/config.php');
include('includes/inc.php');
include('includes/users.inc.php');
include('includes/users.inc-view.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Cache-control" content="no-cache">
<link rel="icon" type="png" href="ico/logo.png" />
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:200">
</head>
<body>
<?php
if (isset($_SESSION['user'])) {
?>
<div class="content">
<div id="main_nav">
<div class="menu_header">Hey, <?php echo $_SESSION['user'];?> [<a href="?logout">LOGOUT</a>]</div>
<div class="menu_header">User</div>
<a href="<?php echo $base_url;?>user/?users"><li>Add user details</li></a>
</div>
<?php
if (file_exists($pagex)) {
include($pagex);
} else {
echo "File: <i>" . $pagex . "</i> does not exist!";
}
?>
</div>
<?php
} else {
include('includes/goback.php');
}
?>
</body>
</html>
该代码通过索引页面,应该设置$ _SESSION,但是没有设置,因此它返回到第一个index.php,即登录表单所在的位置,一旦我再次登录,它就可以工作了。
这是user / incudes / goback.php:
<?php
if(!isset($_SESSION['user'])){
header('Refresh: 0; url='.$base_url.'');
}else{
header('Refresh: 0; url='.$base_url.'user');
}
这可能是我提出的一个很严重的问题,很抱歉,但是我不明白为什么我无法通过第一次尝试设置会话。而且我尝试过使用Cookeies还是一样的结果。我究竟做错了什么?我是否还需要session_start();
?
谢谢