添加用户名和密码错误消息

时间:2012-02-07 04:18:27

标签: php mysql

如果用户输入了错误的用户名或密码,我想添加一条错误消息。目前,如果用户名或密码有误,则只需重新加载登录页面即可。

4 个答案:

答案 0 :(得分:0)

只需修改if语句,看看他们是否成功登录:

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

答案 1 :(得分: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>";
}

&GT;

答案 2 :(得分:0)

使用其他,

    if(Something goes wrong){

    //display the errors


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


    }

无需向您提供可以谷歌搜索的完整代码。

答案 3 :(得分: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";
}
?>