2015-07-03 50 views
-1

我試圖在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 
} 
} 
+0

答案寫了應該解決的主要問題.. – chris85

回答

0

使用header重定向服務器端代碼(請確保您不會輸出/打印/回聲在此之前的東西)。
else if條件加入這個(你在哪裏檢查有效的用戶名和密碼) -

} else if ($uname == $dbuser && $password == $dbpassword) { 
//...All your session assignments 
header("Location: home.php"); 
} 
+0

感謝comment.but當我使用這個它顯示以下錯誤 「警告:不能更改頭信息 - 已經(輸出發送開始頭在C:\ xampp \ htdocs \ koko \ login.php:121)在C:\ xampp \ htdocs \ koko \ login.php行187「 – lahiruk93

+0

@ lahiruk93是的。看起來好像你正在呼應/打印/顯示一些HTML。確保你不會發送任何東西,如果你正在重定向頁面 – Kamehameha

+0

在這種情況下,請確保您的所有表單標籤都在php代碼下面和else標籤內。如果你想重定向到另一個頁面,輸出正確的東西沒有意義? – Kamehameha

0
if (status == 1){ //or whatever stands for logged in 
    header('Location: http://www.yoursite.com/new_page.html') ; 
} 
+0

感謝您的評論。當我使用標題功能它顯示警告味精「不能修改標題信息」。這是爲什麼? – lahiruk93

0

目前其實沒有「重定向」在你的代碼。當你發送表單時,它只是打開「login.php」頁面,就這些。如果你想添加重定向當登錄成功後,只需添加

header('Location: index.php'); 

$_SESSION['username'] = $uname; 
0

這裏有一個答案,應該讓你的網頁功能更好,正確的安全問題。

<?php 
session_start(); 
if (isset($_POST["login"])) { 
    $uname = mysql_real_escape_string($_POST['uname']); 
    $password = mysql_real_escape_string($_POST['password']); 
    $query = mysql_query("select *from user where username='$uname' and password='$password' and `status` <> 0") or die(mysql_error()); 
    $numrows = mysql_num_rows($query); 
    if ($numrows == 0) { 
     echo '<div class="alert alert-danger" style="text-align: center ; font-size: 17px;" role="alert">Sorry the provided credentials were not valid, or you account is invalid.</div>'; 
    } else { 
     $_SESSION['username'] = $uname; 
     header('Location: PAGEADDRESSHERE'); 
    } 
} 

與您當前密碼的問題:
1.用戶輸入直行到您的查詢。這會打開SQL注入。使用http://php.net/manual/en/function.mysql-real-escape-string.php來解決此問題。
2.您應該在與數據庫相同的調用中檢查狀態和密碼。
3.您不應以純文本格式存儲您的密碼。如果惡意用戶訪問您的數據庫,他們將擁有所有用戶密碼。 https://security.stackexchange.com/questions/36833/why-should-i-hash-passwords
4.你永遠不現在就開始你的會話

到重定向...你可以使用PHP頭功能,將用戶重定向,http://php.net/manual/en/function.header.php。路徑應該是絕對的。如果在此函數之後發生進程,則應在其之後使用exit()以確保停止所有處理。

另請注意,mysql_函數已被棄用mysqli_PDO應被使用。

此外,根據您在其他答案中的意見,這就是爲什麼你只是在添加header函數時得到警告。

請記住,在發送任何實際輸出之前,必須調用header(),或者使用普通的HTML標記,文件中的空行或PHP。使用include或require函數或其他文件訪問函數讀取代碼,並在調用header()之前輸出空格或空行是非常常見的錯誤。使用單個PHP/HTML文件時存在同樣的問題。

0
<?php 
if ($_SERVER['REQUEST_METHOD'] == "POST") { 
$uname = mysql_real_escape_string($_POST['uname']); 
$password = mysql_real_escape_string($_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; 
header("location: home.php"); 
?> 
<?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 
} 
} 

?> 
<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>