我有一个登录页面,用户在成功登录帐户后会被重定向到profile.php页面。但是,如果他们只是单击登录按钮而不输入值或者输入错误的值,我希望将它们重定向到index.html页面。我尝试使用header("location: index.html");
,但它没有用。
谁能告诉我如何做到这一点。
登录表单页面是:
<form role="form" action="login.php" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email" name="email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="password">
</div>
<input name="submit" type="submit" value=" Login ">
</form>
Login.php页面是
<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['email']) || empty($_POST['password'])) {
$error = "email or Password is invalid";
}
else
{
// Define $email and $password
$email=$_POST['email'];
$password=$_POST['password'];
// To protect MySQL injection for Security purpose
$email = stripslashes($email);
$password = stripslashes($password);
$email = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
//Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("abc.com", "abc", "abc");
// Selecting Database
$db = mysql_select_db("abc", $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = mysql_query("select * from users where password='$password' AND email='$email'", $connection);
$rows = mysql_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$email; // Initializing Session
header("location: profile.php"); // Redirecting To Other Page
} else {
$error = "email or Password is invalid";
header("location: index.html");
}
mysql_close($connection); // Closing Connection
}
}
?>
答案 0 :(得分:1)
<?php
session_start(); // Starting Session
if (isset($_POST['submit'])) {
try {
if (empty($_POST['email']) || empty($_POST['password'])) {
throw new Exception("email or Password is invalid");
} else {
// Define $email and $password
$email = $_POST['email'];
$password = $_POST['password'];
// To protect MySQL injection for Security purpose
$email = stripslashes($email);
$password = stripslashes($password);
$email = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
//Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("abc.com", "abc", "abc");
// Selecting Database
$db = mysql_select_db("abc", $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = mysql_query("select * from users where password='$password' AND email='$email'", $connection);
$rows = mysql_num_rows($query);
if ($rows != 1)
throw new Exception("email or Password is invalid");
$_SESSION['login_user'] = $email; // Initializing Session
header("location: profile.php"); // Redirecting To Other Page
mysql_close($connection); // Closing Connection
}
}
catch (Exception $e) {
$_SESSION['login_error'] = $e->getMessage();
header("Location: index.html");
}
}
?>
您可以通过$ _SESSION [&#39; login_error&#39;]
收到您的login_error答案 1 :(得分:0)
如果您的两个字段均为空白,则表示您不会重定向任何位置,因此请在$error
之后添加标题
if (empty($_POST['email']) || empty($_POST['password'])) {
header("location: index.html");
}
此外,如果你重定向html页面不需要使用$ error,因为你无法在html页面上获得php变量。
答案 2 :(得分:0)
// Change you conditional statement like ...
if (empty($_POST['email']) || empty($_POST['password'])) {
header("location: index.html");
exit(0);
}
答案 3 :(得分:0)
我尝试使用你的代码并稍微修改它对我来说没问题
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['email']) || empty($_POST['password'])) {
header("location: index.html");
exit(0);
} // ..... and so on