如何解决 php 中的登录错误


How to solve the login error in php

我在php中制作了一个登录页面。 对于数据库,我在这里使用了mysql。如果我想登录,它总是显示消息用户名密码错误...但是已经存在用户名和密码。所以我无法找出问题所在。

http://jsfiddle.net/Sazal/my4wvnvt/

这是我的登录设计页面。

这是下面的检查登录.php代码..

// userName and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['pwd'];
// 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 pass='$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['login_user']=$myusername; // Initializing Session
//$_SESSION['myusername']=$myusername;
//$_SESSION['password']=$mypassword;
//session_register("mypassword");
header("location:home.php");
}
else {
//echo "Wrong Username or Password";
header("Location:index.php?errorMssg=".urlencode("Wrong Username or Password"));
}
?>

表单元素没有名称属性。你不能只依靠"id"。

<input type="text" class="form-control" id="username" placeholder="Enter username">

<input type="text" class="form-control" id="pwd" placeholder="Enter password">

您需要添加它们,因为您要声明

$myusername=$_POST['username'];
$mypassword=$_POST['pwd'];

修改为:

<input name="username" type="text" class="form-control" id="username" placeholder="Enter username">

<input name="pwd" type="text" class="form-control" id="pwd" placeholder="Enter password">

我注意到您可能以纯文本形式存储密码。如果是这种情况,强烈建议不要这样做。

我建议您使用 CRYPT_BLOWFISH 或 PHP 5.5 的password_hash()函数。对于 PHP <5.5,请使用 password_hash() compatibility pack


旁注:您还应该在每个标题后添加exit;

即:

if($count==1 ){
    header("location:home.php");
    exit;
}
else {
//echo "Wrong Username or Password";
    header("Location:index.php?errorMssg=".urlencode("Wrong Username or Password"));
    exit;
}

  • 考虑将mysqli与预处理语句一起使用,或将 PDO 与预处理语句一起使用,它们要安全得多

还需要考虑的几件事是,列长度应足够长,以容纳数据,列类型也是如此。

在存储密码哈希时,我自己充分利用VARCHAR;255.

我承认这是矫枉过正,但这只是我。

尝试:

<input name="username"...>

不:

<input id="username"...>

密码相同。

它是为$_POST数组编制索引的"name"属性的值。

另外,在 mysql 端搜索错误之前,尝试在 php 脚本中var_dump($_POST);,以测试您从 html 表单中实际收到的内容。

您的输入字段没有名称属性使用 name="使用 id 和类不会通过 post 请求
发送提交的值没有名称属性的输入字段,您无法通过 POST 请求获取数据