2012-02-07 73 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"; 
} 
?> 
相关问题