我已经用PHP编写了登录页面的代码。当我提交登录详细信息时,即使我已编写代码以重定向到其他页面,该页面也只是被重定向到自身。请帮我确定错误的位置。
代码看起来像这样
<?php
session_start();
$username = $password= "";
$usernameErr = $passwordErr = "";
$empty = false;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if(empty($_POST["username"]))
{
$empty = true;
$usernameErr="Username is empty. Please try again";
}
else
{
$username = $_POST["username"];
}
if(empty($_POST["password"]))
{
$empty = true;
$passwordErr="Password is empty. Please try again";
}
else
{
$password = $_POST["password"];
}
if(!$empty)
{
$servername = "myxor.in.mysql";
$dbusername = "myxor_in";
$dbpassword = "srikanth177";
$dbname = "myxor_in";
// Create connection
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT firstname,password,package FROM users WHERE username='$username'";
$result = $conn->query($sql);
if (($result->num_rows) > 0)
{
$row = $result->fetch_assoc();
if($username === $row['username'])
{
if($password === $row['password'])
{
echo "came here";
$_SESSION['fname'] = $row['firstname'];
$_SESSION['pack'] = $row['package'];
$_SESSION['login_user'] = $username;
header("Location: http://www.myxor.in/home.php");
}
else
{
$passwordErr = "Invalid Password. Please Try Again";
}
}
} else
{
$usernameErr = "Invalid Username. Please Try Again";
}
}
}
?>
<head>
<style>
td{
color:red;
}
</style>
<title>Login Page</title>
</head>
<body>
<h2 align=center style="color:blue">Login Form</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<table align=center bgcolor=grey >
<tr>
<td><label>UserName :</label>
<td><input id="name" name="username" placeholder="username" type="text">
<td><?php echo $usernameErr?></td>
<tr>
<td><label>Password :</label>
<td><input id="password" name="password" placeholder="**********" type="password">
<td><?php echo $passwordErr?></td>
<tr>
<td colspan=2 align=center><input name="submit" type="submit" value=" Login ">
</table>
</form>
</body>
</html>
答案 0 :(得分:2)
这就是问题所在:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
您正在使用$ _SERVER [&#34; PHP_SELF&#34;]将表单发送到同一个网址(除非您希望将表单发送到您的情况下的其他网址,否则这实际上是有效的) 。将操作更改为要将表单发送到的URL以进行验证。 E.g。
<form action="validate_form.php" method="post">
.....
答案 1 :(得分:0)
试试这个:
<?php
session_start();
include("validations.php");
$username = "";
$password= "";
$usernameErr = $passwordErr = "";
$empty = false;
if(isset($_POST['submit'])
{
if(empty($_POST["username"]))
{
$empty = true;
$usernameErr="Username is empty. Please try again";
}
else
{
$username = $_POST["username"];
}
if(empty($_POST["password"]))
{
$empty = true;
$passwordErr="Password is empty. Please try again";
}
else
{
$password = $_POST["password"];
}
if(!$empty)
{
$servername = "myxor.in.mysql";
$dbusername = "myxor_in";
$dbpassword = "srikanth177";
$dbname = "myxor_in";
// Create connection
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT firstname,password,package FROM users WHERE username='$username'";
$result = $conn->query($sql);
if (($result->num_rows) > 0)
{
$row = $result->fetch_assoc();
if($username === $row['username'])
{
if($password === $row['password'])
{
echo "came here";
$_SESSION['fname'] = $row['firstname'];
$_SESSION['pack'] = $row['package'];
$_SESSION['login_user'] = $username;
header('Location: http://www.myxor.in/home.php');
exit;
}
else
{
$passwordErr = "Invalid Password. Please Try Again";
}
}
} else
{
$usernameErr = "Invalid Username. Please Try Again";
}
}
}
?>
<head>
<style>
td{
color:red;
}
</style>
<title>Login Page</title>
</head>
<body>
<h2 align=center style="color:blue">Login Form</h2>
<form action="this_page.php" method="post">
<table align=center bgcolor=grey >
<tr>
<td><label>UserName :</label>
<td><input id="name" name="username" placeholder="username" type="text">
<td><?php echo $usernameErr?></td>
<tr>
<td><label>Password :</label>
<td><input id="password" name="password" placeholder="********** type="password">
<td><?php echo $passwordErr?></td>
<tr>
<td colspan=2 align=center><input name="submit" type="submit" value=" Login ">
</table>
</form>
</body>
</html>
你有:$username = $password = "";
我将其更正为$username = ""; $password="";
我改变了一些HTML
我在html中更改的是表单操作,而不仅仅是<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
我将其更改为:<form action="yourpagevalidation.php" method="post">
我在header
我将您的if($_REQUEST[]
内容更改为if(isset($_POST['submit']))
答案 2 :(得分:0)
或者你可以这样做:
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
答案 3 :(得分:0)
谢谢你们的帮助。我自己发现了这个错误。错误是我试图从行中访问“用户名”而不在查询中选择用户名。所以它出了故障。我通过在选择列表中添加用户名列来纠正它。再次感谢你。