2012-06-27 66 views
0

我有一個php文件,它檢查用戶數據庫的登錄名和密碼,它工作正常。使用會話和重定向的php錯誤處理

但我無法驗證確切的錯誤顯示,如果用戶不存在或密碼不正確的用戶和錯誤後返回到上一頁,幫我如何顯示這些錯誤。

<?php // access.php 
include_once 'common.php'; 
include_once 'db.php'; 

session_start(); 

$uid = isset($_POST['uid']) ? $_POST['uid'] : $_SESSION['uid']; 
$pwd = isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION['pwd']; 

if(!isset($uid)) { 
    ?> 

    <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
<title>Login</title> 
    <meta http-equiv="Content-Type" 
     content="text/html; charset=iso-8859-1" /> 
<head> 
<style type="text/css"> 
<!-- 
.style1 { 
    font-size: 16px; 
    font-family: Verdana, Arial, Helvetica, sans-serif; 
} 
.style3 { 
    font-size: 12px; 
    font-family: Verdana, Arial, Helvetica, sans-serif; 
} 
--> 
</style> 

    </head> 
<body> 
    <h1 class="style1"> <br><br> Login Required </h1> 
    <span class="style3"><br> 
    You <strong>must login to access this area </strong>of the site. <br> 
    <br> 
    If you are not a registered user, please contact your Admin 
    to sign up for instant access!</span> 
    <p><form method="post" action="<?=$_SERVER['PHP_SELF']?>"> 
    <span class="style3">User ID:&nbsp;&nbsp;&nbsp;&nbsp;  
    <input type="text" name="uid" size="12" /> 
    <br> 
    <br /> 
    Password:</span>  
    <input type="password" name="pwd" SIZE="12" /> 
    <br> 
    <br /> 
    <input type="submit" value="Login" /> 
    </form></p> 
</body> 
</html> 

    <?php 
    exit; 
} 

$_SESSION['uid'] = $uid; 
$_SESSION['pwd'] = $pwd; 

dbConnect("svga"); 
$sql = "SELECT * FROM user WHERE 
     userid = '$uid' AND password = '$pwd'"; 
$result = mysql_query($sql); 
if (!$result) { 
    error('A database error occurred while checking your '. 
     'login details.\\nIf this error persists, please '. 
     'contact [email protected]'); 
    } 

if (mysql_num_rows($result) == 0) { 
    unset($_SESSION['uid']); 
    unset($_SESSION['pwd']); 
    ?> 

    <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
     <head> 
    <title> Access Denied </title> 
    <meta http-equiv="Content-Type" 
     content="text/html; charset=iso-8859-1" /> 
     <style type="text/css"> 
<!-- 
.style1 { 
    font-size: 16px; 
    font-family: Verdana, Arial, Helvetica, sans-serif; 
} 
.style3 { 
    font-size: 12px; 
    font-family: Verdana, Arial, Helvetica, sans-serif; 
} 
--> 
</style> 

    </head> 
    <body> 
    <br/> 
    <br/> 

    <h1 class="style1"> Access Denied </h1> 
    <p class="style3">Your user ID or password is incorrect, or you are not a 
    registered user on this site. To try logging in again, click 
    <a href="<?=$_SERVER['PHP_SELF']?>">here</a>. To access, please contact our Admin  !</a>.</p> 
    </body> 
    </html> 

<?php 
    exit; 
} 
$username = mysql_result($result,0,'fullname'); 
$_SESSION['user'] = mysql_result($result,0,'userid'); 
?> 
+0

唐'告訴用戶哪一個領域,他插錯。這是出於安全原因。只需顯示常見信息,如「不正確的用戶名和/或密碼」。 –

+0

@Rafael Sedrakyan你是對的!但我的要求是,我必須向用戶顯示什麼是錯誤。 – user1114409

回答

1

乾脆把

if(mysql_num_rows($result)==0) { 
header('location:your_page.php'); 
} 

這將您重定向到該頁面,並假定您已經定義error()方法,只是

echo error(); 
+0

您能否解釋如何編寫error()方法以及在哪個腳本中使用 – user1114409

+0

如果我們將行重定向到用戶輸入用戶名的登錄頁面後檢查行數後,它將丟失所有已輸入的數據! – user1114409

+1

爲此,您可以使用會話將數據恢復到您的表單中。 – baig772