由于密码字段而导致注册失败错误?
Posted
技术标签:
【中文标题】由于密码字段而导致注册失败错误?【英文标题】:Registration failed error due to password field? 【发布时间】:2019-05-02 05:08:51 【问题描述】:我的应用在尝试使用用户名和密码注册时收到 registration failed
错误。这些是正在使用的函数。
index.php(尝试使用任何用户名或密码注册时收到registration failed
警报)
// the functions you call inside the switch are found in the api.php file
switch ($_POST['command'])
case "login":
login($_POST['username'], $_POST['password']);
break;
case "register":
register($_POST['username'], $_POST['password']);
break;
api.php
// register API
function register($user, $pass)
//check if username exists in the database (inside the "login" table)
$login = query("SELECT username FROM login WHERE username='%s' limit 1", $user);
if (count($login['result'])>0)
//the username exists, return error to the iPhone app
errorJson('Username already exists');
//try to insert a new row in the "login" table with the given username and password
$result = query("INSERT INTO login(username, pass) VALUES('%s','%s')", $user, $pass);
if (!$result['error'])
//registration is susccessfull, try to also directly login the new user
login($user, $pass);
else
//for some database reason the registration is unsuccessful
errorJson('Registration failed');
//login API
function login($user, $pass)
// try to match a row in the "login" table for the given username and password
$result = query("SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' limit 1", $user, $pass);
if (count($result['result'])>0)
// a row was found in the database for username/pass combination
// save a simple flag in the user session, so the server remembers that the user is authorized
$_SESSION['IdUser'] = $result['result'][0]['IdUser'];
// print out the JSON of the user data to the iPhone app; it looks like this:
// IdUser:1, username: "Name"
print json_encode($result);
else
// no matching username/password was found in the login table
errorJson('Authorization failed');
奇怪的是,当index.php
中的密码参数更改为像register($_POST['username'], $_POST['_Any_Random_String']);
这样的任何随机字符串时,注册和登录过程仍然有效。唯一的问题是密码没有插入数据库,可以登录选择的用户名和任何组成的密码。
case "login":
//authenticates user but password isn't uploaded
login($_POST['username'], $_POST['_Any_Random_String']);
break;
//authenticates user but password isn't uploaded
case "register":
register($_POST['username'], $_POST['_Any_Random_String']);
break;
编辑 上面的代码允许用户joey
使用用户名joey
和输入的任何密码进行注册和登录。如果 joey 使用密码 1234
注册,即使他使用密码 1234
注册,他也可以在登录时使用密码 12345678
或任何密码登录。此外,密码1234
或密码字段中的任何输入都不会存储到数据库中。
应用如何根据需要通过$_POST['password']
发布密码?
更新
查尔斯代理输出
command register
username fxjzfnfx
password ®0i!0öºkp«)ÛèGS¡
"error": "Registration failed"
【问题讨论】:
您显然不会在将密码存储到数据库之前对其进行哈希处理,这是一种非常糟糕的做法。除此之外,我真的不明白你的问题是什么。您应该更详细地说明哪些有效,哪些无效。 这似乎也很奇怪,但应用程序就是这样来的。我用一个用例编辑了这个问题。基本上,当我尝试向register($_POST['username'], $_POST['password']);
注册时,它不起作用。但是当密码参数设置为空字符串时,它可以工作,但没有密码存储在数据库中。
您尝试过什么调试问题? query
函数有什么作用?它会检查任何错误吗? SQL 注入怎么样 - 你有没有想过保护你的代码免受它的影响?
我检查了错误日志以了解它的内容。该查询应该使用给定的用户名和密码在“登录”表中插入一个新行。它确实通过 else //for some database reason the registration is unsuccessful errorJson('Registration failed');
检查错误。登录成功后,我将保护代码。
【参考方案1】:
替换这个
//try to insert a new row in the "login" table with the given username and password
$result = query("INSERT INTO login(username, pass) VALUES('%s','%s')", $user, $pass);
到
//try to insert a new row in the "login" table with the given username and password
$result = query("INSERT INTO login(username, pass) VALUES(" . $user . "," . $pass . ")");
然后替换这个
// try to match a row in the "login" table for the given username and password
$result = query("SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' limit 1", $user, $pass);
到
// try to match a row in the "login" table for the given username and password
$result = query("SELECT IdUser, username FROM login WHERE username=". $user . " AND pass=" . $pass ." limit 1");
【讨论】:
我将使用错误日志中的结果更新问题。这很奇怪,因为它根本没有在错误日志中显示任何内容。唯一显示错误的是应用程序中显示registration failed
的警报。【参考方案2】:
$result = query("INSERT INTO login(username, pass) VALUES('%s','%s')", $user, $pass);
在这里检查您的最后一个查询正在执行什么。你是否得到这两个值。您的表中的某些其他字段也可能是必需的。
【讨论】:
是的。只有用户名被发布到数据库。我会检查 Charles 代理,看看它说什么并更新问题。 “检查”是什么意思?这如何解决问题?以上是关于由于密码字段而导致注册失败错误?的主要内容,如果未能解决你的问题,请参考以下文章
SQL server无法启动服务,提示“错误1069: 由于登录失败而无法启动服务”