本节使身份验证功能
<?php
$authentication = function()
{ try{
$app='slim'slim:: getInstance();
$user= $app->request-> headers ->get('HTTP_USER');
$pass= $app->request-> headers ->get('HTTP_PASS');
$pass= sha1($pass);
$connection= getconnection();
$dbh= $connection-> prepare(" SELECT* FROM keys where user=? AND pass=? ");
$dbh->execute();
$users= $dbh-> fetchAll(PDO::FETCH_OBJ);
//print_r($users);
$app-> response->headers->set("content-type","application/json");
$app-> response->status(800);
$app-> response->body(Json_encode($users));
if ($authentication === false)
{
$app->halt(401);
}
}
catch(PDOexception $e)
{
echo"error". $e-> getmessage();
}
};
本节用于调用不同id的身份验证
$app-> get("/users/:fb_id", $authentication ,function($fb_id) use($app)
{
try{
$connection= getconnection();
$dbh= $connection-> prepare("SELECT* FROM users where fb_id= ?");
$dbh->bindparam(1,$fb_id);
$dbh->execute();
$users= $dbh-> fetchobject();
print_r($users);
//$app-> response->headers->set("content-type","application/json");
//$app-> response->status(200);
//$app-> response->body(Json_encode($users));
}
catch(PDOexception $e)
{
echo"error". $e-> getmessage();
}
});
?>
这里没有以传统方式使用准备好的语句。
把这些行放在你的代码中,我认为它会很好地工作
$connection= getconnection();
$dbh= $connection-> prepare(" SELECT* FROM keys where user=:user AND pass=:pass");
$dbh->bindParams(':user',$user);
$dbh->bindParams(':pass',$pass);
$dbh->execute();
$users= $dbh-> fetchAll(PDO::FETCH_OBJ);
您的方法也可以,但需要在execute()
中发送一个数组作为参数。文档中我的代码的更多信息和替代方案。
它显示了这个"调用未定义的方法PDOStatement::bindParams()"
代码在这里
$authentication = function()
{ try{
$app='slim'slim:: getInstance();
$user= $app->request-> headers ->get('HTTP_USER');
$pass= $app->request-> headers ->get('HTTP_PASS');
$pass= sha1($pass);
$connection= getconnection();
$dbh= $connection-> prepare(" SELECT* FROM keys where user=:user AND pass=:pass");
$dbh->bindParams(':user',$user);
$dbh->bindParams(':pass',$pass);
$dbh->execute();
$users= $dbh-> fetchAll(PDO::FETCH_OBJ);