登录后无法重定向

时间:2019-01-22 18:16:59

标签: php header

成功登录后,我试图重定向到管理页面。

尝试使用ob_start(),但是它也无法正常工作。成功登录后,页面会重新加载到login.php。

会话已设置,我可以访问admin.php,但前提是必须手动输入URL。 这是login.php:

<body>
	<?php include('adminServer.php') ?>

	<form method="post" action="login.php">
		<div class="errorLogin">
			<?php include('errors.php'); ?>
		</div>
		<div class="logBody">
			<div class="labelLog">
				<label>Username</label>
				<label>Password</label>
			</div>
			<div class="inputLog">
				<input type="text" name="username">
				<input type="password" name="password">
			</div>
		</div>
		<div class="footerLog">
			<button type="submit" class="btn" name="login_user">Login</button>
		</div>
	</form>
</body>

和adminServer.php:

<?php ob_start();
session_start();
$username = "";
$errors = array(); 
$db = mysqli_connect('localhost', 'root', '', 'admins');

// LOGIN USER
if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);
  if (empty($username)) {
  	array_push($errors, "Username is required");
  }
  if (empty($password)) {
  	array_push($errors, "Password is required");
  }
  if (count($errors) == 0) {
  	$password = md5($password);
  	$query = "SELECT * FROM adminlist WHERE username='$username' AND password='$password'";
  	$results = mysqli_query($db, $query);
  	if (mysqli_num_rows($results) == 1) {
  	  $_SESSION['username'] = $username;
      $_SESSION['success'] = "You are now logged in";
  	  header('location: admin.php');
  	}else {
  		array_push($errors, "Wrong username/password combination");
  	}
  }
}
ob_end_flush();
?>

查看堆栈上的类似文章后,我发现必须在脚本开始时调用输出缓冲。但这仍然不能解决问题。

任何帮助都非常必要!

0 个答案:

没有答案