2013-06-03 217 views
1

快速的問題...我現在登錄頁面正在工作,這樣當用戶輸入正確的信息時,它會將它們重定向到會話中的主頁。當他們輸入錯誤的信息時,只會將它們重定向回登錄頁面。驗證用戶登錄php

我在做什麼,是當他們輸入錯誤的信息,我希望它仍然重定向到登錄頁面,但我希望它說無效登錄,請再試一次。我想呼應的是,但它只是刷新頁面上沒有回聲..

登錄頁面

<?php include '../../view/header.php'; ?> 
<div id="main"> 
<?php 

// Inialize session 
session_start(); 

// Check, if user is already login, then jump to secured page 
if (isset($_SESSION['username'])) { 
header('Location: ../../admin'); 
} 

?> 

<html> 

<head> 
<title>Login</title> 
</head> 

<body> 

<h3>Admin Login</h3> 

<table> 
<form method="POST" action="loginproc.php"> 
<tr><td>Username</td><td>:</td><td><input type="text" name="username" size="20"></td></tr> 
<tr><td>Password</td><td>:</td><td><input type="password" name="password" size="20"></td></tr> 
<tr><td>&nbsp;</td><td>&nbsp;</td><td><input type="submit" value="Login"></td></tr> 
</form> 
</table> 

</body> 

</html> 
</div> 
<?php include '../../view/footer.php'; ?> 

loginproc.php

<?php 

// Inialize session 
session_start(); 

// Include database connection settings 
require_once('database.php'); 

// Retrieve username and password from database according to user's input 
$login = mysql_query("SELECT * FROM user WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string($_POST['password']) . "')"); 




// Check username and password match 
if (mysql_num_rows($login) == 1) { 
// Set username session variable 
$_SESSION['username'] = $_POST['username']; 
// Jump to secured page 
header('Location: ../../admin'); 
} 
else { 
// Jump to login page 

header('Location: index.php'); 

} 

?> 
+0

您可以重定向添加GET PARAMS:'頭( '位置:網址/網頁loginfail = 1?');' –

+0

知道會話可以輕鬆劫持如果你不增加更多的安全性(例如檢查會話IP和REMOTE_ADDR等等) –

+0

這個問題每週都會被問到幾次,如果你搜索關於這個主題的以前的問題,你肯定會找到答案。 – Jocelyn

回答

0
<?php include '../../view/header.php'; ?> 
<div id="main"> 
<?php 

// Inialize session 
session_start(); 

// Check, if user is already login, then jump to secured page 
if (isset($_SESSION['username'])) { 
header('Location: ../../admin'); 
} 
else if (isset($_GET['loginfail']) { 
    echo "Please try again later"; 
} 
?> 

<html> 

<head> 
<title>Login</title> 
</head> 

<body> 

<h3>Admin Login</h3> 

<table> 
<form method="POST" action="loginproc.php"> 
<tr><td>Username</td><td>:</td><td><input type="text" name="username" size="20"></td></tr> 
<tr><td>Password</td><td>:</td><td><input type="password" name="password" size="20"></td></tr> 
<tr><td>&nbsp;</td><td>&nbsp;</td><td><input type="submit" value="Login"></td></tr> 
</form> 
</table> 

</body> 

loginproc.php

<?php 

// Inialize session 
session_start(); 

// Include database connection settings 
require_once('database.php'); 

// Retrieve username and password from database according to user's input 
$login = mysql_query("SELECT * FROM user WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string($_POST['password']) . "')"); 




// Check username and password match 
if (mysql_num_rows($login) == 1) { 
// Set username session variable 
$_SESSION['username'] = $_POST['username']; 
// Jump to secured page 
header('Location: ../../admin'); 
} 
else { 
// Jump to login page 

header('Location: index.php?loginfail=1'); 

} 

?> 
1

你不應該重定向之前使用的任何輸出。它可能工作,但它也可以打印出錯誤而不用重定向。

至於重定向和輸出。您可以將一些信息存儲到SESSION,根據SESSION重定向然後打印出錯誤。如果您的錯誤報告只是簡單的,您可以使用GET並通過重定向頁面在參數中傳遞。