我正在通过登录表单使用会话,但在尝试登录时不会重定向到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=kitchen&login=Login
请帮忙......
答案 0 :(得分:0)
http://localhost/malala/admin/login.php?user_name=kitchen&user_pass=kitchen&login=Login
将变量发送为GET
,您将其视为POST