我登录后没有转到index.php,页面只是刷新我正在使用mysql和数据库的详细信息都在配置文件
这是login.php
<form action="" method="post">
UserName: <input type="text" name="uname"><br>
Password: <input type="text" name="psw1"><br>
<button type="submit" name="submit">Login</button>
</form>
<?php
require 'config1.php';
if(isset($_POST["submit"])){
$uname = $_POST["uname"];
$psw1 = $_POST["psw1"];
$result = mysqli_query($conn, "SELECT * FROM Users WHERE Username = '$uname'");
$row = mysqli_fetch_assoc($result);
if(mysqli_num_rows($result) > 0){
if($psw1 == $row["Password"]){
$_SESSION["login"] = true;
$_SESSION["id"] = $row["id"];
header("Location: index.php");
}
else{
echo
"<script> alert('wrong password'); </script>";
}
}
else{
echo
"<script> alert('User not regitered'); </script>";
};
}
?>
这是index.php,因此在成功登录后我应该去的页面
`<?php
require 'config1.php';
if(!empty($_SESSION["id"])){
$id = $_SESSION["id"];
$result = mysqli_query($conn, "SELECT * FROM Users WHERE id = $id");
$row = mysqli_fetch_assoc($result);
}
else{
header("Location: login.php");
}
?>
<!DOCTYPE html>
<html>
<body>
<h1>Welcome <?php echo $row["name"]; ?></h1>
<a href="logout.php">Log out</a>
</body>
</html>`
在执行标题之前发出html。事实并非如此。在浏览器接收到一些html之后,您不能更改页面,在本例中是表单。所以这样重新组织。
<?php
require 'config1.php';
if(isset($_POST["submit"])){
$uname = $_POST["uname"];
$psw1 = $_POST["psw1"];
$result = mysqli_query($conn, "SELECT * FROM Users WHERE Username = '$uname'");
$row = mysqli_fetch_assoc($result);
if(mysqli_num_rows($result) > 0){
if($psw1 == $row["Password"]){
$_SESSION["login"] = true;
$_SESSION["id"] = $row["id"];
header("Location: index.php");
exit;
}
else{
echo
"<script> alert('wrong password'); </script>";
}
}
else{
echo
"<script> alert('User not regitered'); </script>";
};
}
?>
<form action="" method="post">
UserName: <input type="text" name="uname"><br>
Password: <input type="text" name="psw1"><br>
<button type="submit" name="submit">Login</button>
</form>
注意这里有一个出口在头文件之后,因为你不希望文件的其余部分被执行。
注意
假设config1.php不回显或发出任何html