我的网站有问题,问题在于: 当我尝试登录并输入正确的登录信息时,我一直被卡在process_login页面上并且标题不想将我重定向回索引页面...这只发生在我的网页上,在localhost上一切都很顺利.. 代码:
登录模式
<div class="modal fade" id="login" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<center><div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<div class="alert alert-info">Login page</div>
</div></center>
<div class="modal-body">
<form role="form" action="process_login.php" method="post">
<div class="form-group">
<label for="exampleInputUsername">Username</label>
<input type="text" name="username" class="form-control" id="username_login" placeholder="Enter username"><span id="span_username_login"></span>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" name="password" class="form-control" id="password_login" placeholder="Password"><span id="span_password_login"></span><br/>
</div>
<a href="#recover" data-toggle="modal" ><p>I've forgotten my password</a></p>
<div class="modal-footer">
<center><input type="submit" name="submit" class="btn btn-danger" value="Sign in"/></center>
</div>
</form>
</div>
</div>
</div>
</div>
process_login.php页面
<?php
require_once 'includes/initialize.php';
require_once 'classes/database.php';
require_once 'classes/bcrypt.php';
require_once 'classes/user.php';
if(isset($_POST['submit']))
{
$username = filter_input(INPUT_POST, 'username' , FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password' , FILTER_SANITIZE_STRING);
//if input fields ain't filled , redirecting back to index page
if(empty($username) || empty($password))
{
$_SESSION['emptyfields'] = '';
header("Location: index.php");
}
//if that particular username doesnt exist in database , redirecting back to index page
if($userclass->userExists($username) == false)
{
$_SESSION['usernamedoesntexist'] = '';
header("Location: index.php");
}
//if that particular email isnt confirmed yet , redirecting back to index page
if($userclass->emailConfirmed($username) == false)
{
$_SESSION['emailnotconfirmed'] = '';
header("Location: index.php");
}
if($userclass->banned($username) == false)
{
//if that particular username is banned , redirecting back to index page
$_SESSION['banned'] = '';
header("Location: index.php");
}
$login = $userclass->login($username, $password);
if($login == false)
{
$_SESSION['errorwithlogin'] = ''; //incorect username - password combo
header("Location: index.php");
}
else
{
$_SESSION['user_auth'] = TRUE; //corect username - password combo
$_SESSION['user_id'] = $login;
$_SESSION['loginsuccessfull'] = '';
header("Location: index.php");
}
}
else
{
$_SESSION['errorwithprocessing'] = '';
header("Location: index.php");
}
?>
<?php
unset($_SESSION['errorwithprocessing']);
unset($_SESSION['loginsuccessfull']);
unset($_SESSION['errorwithlogin']);
unset($_SESSION['banned']);
unset($_SESSION['emailnotconfirmed']);
unset($_SESSION['usernamedoesntexist']);
unset($_SESSION['emptyfields']);
?>
class user.php页面(与登录有连接的行)
public function login($username, $password)
{
global $db;
global $bcrypt;
$stm = $db->connection->prepare("SELECT `user_id`,`password` FROM `users` WHERE `username` = ?");
$stm->bindValue(1, $username);
try
{
$stm->execute();
$data = $stm->fetch();
$stored_password = $data['password'];
$id = (int) $data['user_id'];
if($bcrypt->verify($password, $stored_password) === true)
{
return $id;
}
else
{
return false;
}
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
欢迎任何帮助,提前谢谢..
答案 0 :(得分:0)
从我所看到的情况来看,我假设您在进入重定向代码之前正在开始会话。也许在你收录的一个文件中?无论如何,在调用header()
之前,您需要调用所有session_start()
函数。根据我的经验,不这样做会导致不可预测的结果,具体取决于我运行的PHP版本和服务器。
但是,我确实理解这并不总是一个选项,考虑到这一点,我使用以下代码作为header("Location:index.php")
的解决方法;这将使用javascript进行重定向。
echo '<script type="text/javascript">window.location="index.php";</script>';