我正在尝试创建一个与SQL数据库连接的基本登录页面。
但是,header('location: profile.php');
没有重定向到指定的页面,而是admin_login页面刷新为空白状态。我花了一段时间在这里浏览了无数其他与类似问题相关的解决方案,但似乎没有解决我的问题。
干扰线似乎是在admin_login.php的顶部:
if ($_POST['submit']) {
当我移动上面的标题调用时,标题会执行并重定向到profile.php页面。
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
if ($_POST['submit']) {
include_once("connection.php");
$username = strip_tags($_POST["username"]);
$password = strip_tags($_POST["password"]);
$sql = "SELECT id, username, password FROM login WHERE username = '$username' AND activated = '1' LIMIT 1";
$query = mysqli_query($dbCon, $sql);
if ($query) {
$row = $mysqli_fetch_row($query);
$userID = $row[0];
$dbUsername = $row[1];
$dbPassword = $row[2];
}
if ($username == $dbUsername && $password == $dbPassword) {
$_SESSION['username'] = $username;
$_SESSION['id'] = $userID;
header('location: /profile.php');
exit();
} else {
echo "Incorrect username/password combo";
exit();
}
}
?>
<?php
$pageTitle = "Casa Mirador | Admin";
include_once('inc/header.php');
?>
<h2 style="text-align: center;">Admin Login</h2>
<div class="login_section_one">
<div class="wrapper">
<form method="POST" action="admin_login.php" id="admin_form">
<table class="form_login">
<tr>
<th>
<label for = "username"> Username </label>
</th>
<td>
<input type="text" name="username" id="username">
</td>
</tr>
<tr>
<th>
<label for = "password"> Password </label>
</th>
<td>
<input type="text" name="password" id="password">
</td>
</tr>
</table>
<input type="submit" id="submit" value="Send">
</form>
</div>
<?php
include_once('inc/footer.php');
?>
<?php
$dbCon = mysqli_connect('localhost', 'root', 'password', 'company');
if (mysqli_connect_errno()) {
/* echo "failed to connect: " . mysqli_connect_error(); */
exit();
}
?>
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
if (isset($_SESSION['id'])) {
$userID = $_SESSION['id'];
$username = $_SESSION['username'];
} else {
header('Location: index.php');
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>SECRET LOGIN PAGE</title>
</head>
<body>
LOGIN SUCCESS!
</body>
</html>