2012-02-07 74 views
-1

如果用戶輸入了錯誤的用戶名或密碼,我想添加一條錯誤消息。目前如果用戶名或密碼錯誤,它只是重新加載登錄頁面而沒有錯誤。添加用戶名和密碼錯誤消息

+0

我看不到任何給定的錯誤消息。即使你應該寫一個錯誤信息的其他條件 – 2012-02-07 04:26:02

+0

@jacob請看我的答案。我已經提供了一個從數據庫中創建表的簡單教程鏈接,創建一個HTML到PHP腳本。 – 2012-02-07 11:16:17

回答

0

只需修改您的if語句,看看他們是否有過成功登錄:

if(mysql_num_rows($login) == 1) 
{ 
    // Successful login 
    $_SESSION['username'] = $_POST['username']; 
    ... 
} 
else 
{ 
    // Invalid login 
    echo "Your username or password are incorrect!"; 
} 
+0

我是否將其餘的代碼插入...? – Jacob 2012-02-07 05:15:02

+0

你能檢查我是否做得對,並在必要時進行編輯:http://pastebin.com/V1urcqBe – Jacob 2012-02-07 05:42:31

+0

@Jacob - 它看起來像你需要這樣的東西:http://pastebin.com/c9awk0X4 – nickb 2012-02-07 06:20:54

0
if(mysql_num_rows($login) == 1) 
{ 
$SESSION['username'=$_POST['username']; 

//Successfull login redirect to home page 
} 
else 
{ 
// incorrect username or password 
$incorrectLogin_flag =1; 
} 

然後在HTML

<?php 

echo "Username : <input type='text' name='username' />"; 

echo "Username : <input type='password' name='password' />"; 

echo " <input type='submit' value='Login' />"; 

echo " <input type='submit' value='Cancel' />"; 

if($incorrectLogin_flag ==1) 
{ 
echo" <label style='color:red;'> Username or Password incorrect.</label>"; 
} 

>

0

使用別的,

if(Something goes wrong){ 

    //display the errors 


    }elseif(all goes right){ 
    //login 
    //redirect 


    } 

沒有必要給你完整的代碼,你可以谷歌它。

0

這可能是使用php和mysql創建簡單驗證的基本教程。

REFERENCE for PHP Login script tutorial

示例代碼段:

<?php 
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection) 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; 
$result=mysql_query($sql); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 
// If result matched $myusername and $mypassword, table row must be 1 row 

if($count==1){ 
// Register $myusername, $mypassword and redirect to file "login_success.php" 
session_register("myusername"); 
session_register("mypassword"); 
header("location:login_success.php"); 
} 
else { 
echo "Wrong Username or Password"; 
} 
?> 
相關問題