我有形式登錄我的index.php和的login.php 我檢查是否存在錯誤。
我想$error_txt
下面的信息返回到index.php文件,並顯示該消息是我的代碼:
的index.php:
<form action="login.php" method="post">
<input type="text" name="username" placeholder="Username" id="user-input" /><br />
<input type="password" name="password" placeholder="Password" id="user-input" /><br />
<input type="submit" name="login" value="Login" id="user-submit" class="btn btn-primary" style="color:white;"/><br />
<a href="register.php">Register</a>
<?php echo $error_txt; ?> <!-- --------- Here I Want show the error message -->
</form>
的login.php:
<?php
if(isset($_POST['login']))
{
$username = mysql_escape_string($_POST['username']);
$password = mysql_escape_string($_POST['password']);
$errors = 0;
try
{
if(strlen($username) == 0)
{
throw new Exception ("The username is empty");
}
if(strlen($password) == 0)
{
throw new Exception ("The password is empty");
}
include ("web/config.php");
$usernameTrueQuery = mysql_query("SELECT * FROM `users` WHERE `Username` = '$username' AND `Password` = '$password' LIMIT 1;");
$usernameTrue = mysql_num_rows($usernameTrueQuery);
if($usernameTrue == 0)
{
throw new Exception ("The username or password is incorrect");
}
} catch (Exception $loginError) {
echo "<div class='alert alert-warning'>
<strong>Error:</strong> ".$loginError->getMessage()."</div> \n";
$errors = 1;
$error_txt = $loginError->getMessage();
}
if($errors == 0)
{
echo "<div class='alert alert-success'>
<strong>Success</strong> Welcome {$username}!</div> \n";
}
}
return $error_txt;
header("Location: /index.php");
?>
感謝.. :)
可能不是重定向,您可以在登錄頁面中「包含」登錄表單,如果出現錯誤,則顯示它重新顯示錶單。您可以將表單本身保存在單獨的文件中,並將其包含在index.php和login.php中。 – GolezTrol
@GolezTrol好idiea,但我想要一些頁面登錄和更多的代碼。謝謝 – UnderPhp