我試圖在login.php表單中創建一個登錄功能。如果登錄成功,我想重定向到index.php頁面。但如果在登錄時出現任何錯誤,它應該重定向到同一頁面(在這種情況下,它應該是login.php)。我怎麼能實現這個?如果沒有錯誤,如何將頁面重定向到主頁?
這是我的表單設計代碼。根據這段代碼總是重定向到login.php。
<form name="login" method="POST" action="login.php">
<table class="table">
<tr>
<td>Username</td>
<td><input type="text" name="uname" class="form-control" placeholder="Username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" class="form-control" placeholder="Password"></td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" name="login" class="btn btn-primary" value="Login">
</td>
</tr>
</table>
</form>
這是登錄
if (isset($_POST["login"])) {
$uname = $_POST['uname'];
$password = $_POST['password'];
$query = mysql_query("select *from user where username='$uname'") or die(mysql_error());
$numrows = mysql_num_rows($query);
if ($numrows != 0) {
while ($row = mysql_fetch_assoc($query)) {
$dbuser = $row['username'];
$dbpassword = $row['password'];
$status = $row['status'];
}
if ($status == 0) {
?>
<div class="alert alert-danger" style="text-align: center ; font-size: 17px;" role="alert">Your account is not yet activated.Please check your email to activate account.</div>
<?php
} else if ($uname == $dbuser && $password == $dbpassword) {
$_SESSION['username'] = $uname;
?>
<?php
} else {
?>
<div class="alert alert-danger" style="text-align: center ; font-size: 17px;" role="alert">Incorrect Password.</div>
<?php
}
} else {
?>
<div class="alert alert-danger" style="text-align: center ; font-size: 17px;" role="alert">Incorrect Email.</div>
<?php
}
}
答案寫了應該解決的主要問題.. – chris85