[英]PHP: Page not redirected back to the login page
I have a login.html
file with a form
having username
and password
field and on submit button
it checks the values from the form with the mysql database
. 我有一个带有username
和password
字段的form
的login.html
文件,在submit button
它使用mysql database
检查表单中的值。
My PHP code is in different file called login.php
我的PHP代码在另一个名为login.php
文件中
The problem is that it works fine when i put correct username and password. 问题是,当我输入正确的用户名和密码时,它工作正常。 But if i put wrong inputs or keep them empty then instead if redirecting to the login.html
page it redirects to login.php
page. 但是,如果我输入了错误的输入或将它们保留为空,则如果重定向到login.html
页面,它将重定向到login.php
页面。
login.php login.php
<?php
### Check if the Submit button is clicked
if (isset($_POST['btnSubmit']))
{
# code...
//$uname=$_POST['uname'];
//$pswd=$_POST['pswd'];
#### Connection to mySQL ####
$conn=mysqli_connect("localhost","root","","vishal") or die("Unable to connect to MySQL");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($conn,"SELECT * FROM login where username = '$_POST[uname]' AND password = '$_POST[pswd]'") or die(mysql_error());
$row = mysqli_fetch_array($result) or die(mysql_error());
$username=$row['username'];
$password=$row['password'];
if (!empty($username) AND !empty($password))
{ # code...
//$_SESSION['username'] =$password;
header('location:welcome.html');
}
else //(empty($username) AND empty($password))
{ # code...
header('location:login.html');
die();
}
}
?>
login.html login.html
<form action="login.php" name="login_form" method="post" enctype="multipart/form-data" target="_self">
<fieldset style="width:300px; margin:auto; margin-top:100px; border:solid 2px #900; background-color:#CCC; border-radius: 50px">
<h1 style="color:#306;"> <center> Login Form </center></h1> <br> <br>
<div style="margin-left:30px;">
<table>
<tr>
<td align="right"> <label for="uname"> <strong>Username :</strong> </label> </td>
<td> <input type="text" title="Username" id="uname" name="uname" placeholder="Username" /> </td>
</tr>
<tr><td> </td><td> </td> </tr>
<tr>
<td align="right"> <label for="pswd"> <strong>Password :</strong> </label> </td>
<td> <input type="password" title="Password" id="pswd" name="pswd" placeholder="Password" /> </td>
</tr>
<tr><td><br /> </td><td> <br /></td> </tr>
<tr>
<td></td>
<td> <input type="submit" title="Submit" name="btnSubmit" id="btnSubmit" value="Submit" style="width:100px; height:26px; font-weight:bold;" /> </td>
</tr>
<tr><td><br /> </td><td> <br /></td> </tr>
</table>
</div>
</fieldset>
</form>
use this code : 使用此代码:
$result = mysqli_query($conn,"SELECT * FROM login where username = '$_POST[uname]' AND password = '$_POST[pswd]'") or die(mysql_error());
$row_count = mysql_num_rows($result);
if($row_count==1) {
$row = mysqli_fetch_array($result) or die(mysql_error());
$username=$row['username'];
$password=$row['password'];
header('location:welcome.html');
} else {
header('location:login.html');
die();
}
You have to do like this in login.php
您必须在login.php
这样做
<?php
### Check if the Submit button is clicked
if (isset($_POST['btnSubmit']))
{
$conn=mysqli_connect("localhost","root","","vishal") or die("Unable to connect to MySQL");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(!empty($_POST['uname']) && !empty($_POST['pswd'])) {
$result = mysqli_query($conn,"SELECT * FROM login where username = '$_POST[uname]' AND password = '$_POST[pswd]'") or die(mysql_error());
$row_count = mysqli_num_rows($result);
if($row_count==1) {
$row = mysqli_fetch_array($result) or die(mysql_error());
$username=$row['username'];
$password=$row['password'];
header('location:welcome.html');
} else {
header('location:login.html');
die();
}
} else {
header('location:login.html');
die();
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.