PHP PDO SESSION无法重定向

PHP PDO SESSION not able to redirect

本文关键字:重定向 SESSION PDO PHP      更新时间:2023-09-26

我通过登录表单使用会话,但当尝试登录时,它没有重定向到index.php。请告诉我哪里错了。以下是我的索引和登录代码

         <?php 
         session_start();
           if (!isset($_SESSION['user_name'])) {
            header("location: login.php");
             }
             else{
              echo "welcome $user_name";
             ?>

跟随html

<!DOCTYPE html>
<html>
<head>
	<title>Admin Panel</title>
	<link rel="stylesheet" type="text/css" href="admin_style.css">
</head>
<body>
<h1>Welcome To Home Page</h1>
</body>
</html>

           <?php } ?>

下面的login.php代码与表单

            <?php
             session_start();
            ?>

<!DOCTYPE html>
<html>
<head>
	<title>Admin Login</title>
</head>
<body>
<form action="login.php" action="post">
	<table width="400" align="center" border="20">
	<tr>
		<td colspan="5" align="center" bgcolor="gray"><h1>Admin Login</h1></td>
	</tr>
	<tr>
		<td align="right">User Name:</td>
		<td><input type="text" name="user_name"></td>
	</tr>
	<tr>
		<td align="right">User Password</td>
		<td><input type="password" name="user_pass"></td>
	</tr>
	<tr>
		<td align="center" colspan="5"><input type="submit" name="login" value="Login"></td>
		
	</tr>
		
	</table>
</form>
</body>
</html>

    <?php 
include("connect.php");
if(isset($_POST['login'])) {
    $user_name = $_POST['user_name'];
    $user_pass = $_POST['user_pass'];
    $sth = $con->prepare("SELECT * FROM admin_login WHERE user_name='$user_name' AND user_password='$user_pass'");

     $sth->execute();

    if($sth->rowCount() > 0) {
        $_SESSION['user_name'] = $user_name;
        header("Location: index.php"); // Redirecting To index Page
        } else {
       echo "<script>alert('Username or Password is invalid')</script>";
       }  
}

?>

通过这段代码,我只能在地址栏上看到,没有javascript警报:http://localhost/malala/admin/login.php?user_name=kitchen&user_pass=厨房&login=登录

请帮忙。。。。

http://localhost/malala/admin/login.php?user_name=kitchen&user_pass=kitchen&login=Login将变量作为GET发送,您正在将其作为POST