Php 如何在Slim API中从reques tinside my sql注入数据
我有一个纤薄的API,我需要做一个函数来验证用户名/密码。我正在使用POST,我想将用户名插入sql中的凭据中。如果我使用GET,我知道怎么做,但我使用的是POST。我如何做到这一点:Php 如何在Slim API中从reques tinside my sql注入数据,php,sql,angularjs,Php,Sql,Angularjs,我有一个纤薄的API,我需要做一个函数来验证用户名/密码。我正在使用POST,我想将用户名插入sql中的凭据中。如果我使用GET,我知道怎么做,但我使用的是POST。我如何做到这一点: function authenticate($req, $resp, $args) { $credentials = json_decode($req->getBody()); $sql = "SELECT * FROM ict_users WHERE usr_username = 'Ins
function authenticate($req, $resp, $args) {
$credentials = json_decode($req->getBody());
$sql = "SELECT * FROM ict_users WHERE usr_username = 'Insert the username here'";
try {
$db = DB_Connection();
$stmt = $db->query($sql);
$password = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
//check if the two password fits (use the password var)
//create a session_key
//Store the session_key in the DB
//return the session_key
}
我在AngularJS服务中使用以下函数调用此函数:
$http.post(appConfig.apiURL + '/authenticate', credentials)
类似这样的事情。我想出来了:
function authenticate($req, $resp, $args) {
$credentials = json_decode($req->getBody());
$sql = "SELECT usr_password FROM ict_users WHERE usr_username='".$credentials->username."'";
try {
$db = DB_Connection();
$stmt = $db->query($sql);
$password = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
if(empty($password)){
echo '{"error":"login_failed"}';
}
else {
if (password_verify($credentials->password, $password[0]->usr_password)) {
echo '{"error":"login_success"}';
}
else {
echo '{"error":"login_failed"}';
}
}
}
catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
听起来你好像把它弄粗了。插入代码,看看会发生什么。我只想返回成功或失败,但我不知道您打算如何管理您的用户授权。你也可以使用Symfony的安全包。这很复杂,但一旦你开始,它就相当酷了。问题是我已经尝试插入一些代码,但没有任何效果。除非我们能看到代码,否则没有多少人能帮你。我已经编写了dmy代码,并使问题更具体
function authenticate($req, $resp, $args) {
$credentials = json_decode($req->getBody());
$sql = "SELECT usr_password FROM ict_users WHERE usr_username='".$credentials->username."'";
try {
$db = DB_Connection();
$stmt = $db->query($sql);
$password = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
if(empty($password)){
echo '{"error":"login_failed"}';
}
else {
if (password_verify($credentials->password, $password[0]->usr_password)) {
echo '{"error":"login_success"}';
}
else {
echo '{"error":"login_failed"}';
}
}
}
catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}