douzhi2988 2016-02-26 14:24
浏览 31
已采纳

如何使用基本身份验证在php中授权rest api

I am developing a rest API for login and I not its working means I can fetch data from the server and send response to the user. Now I want to implement basic Authentication with it. I am very new for Rest API development and Slim. So please Help me in that.

$app->get('/users', function() use($app, $db)
{

$app->response()->header("Content-Type", "application/json");

$req=$app->request();
$pass = $req->params('pass'); // Getting parameter with names
$email = $req->params('email'); // Getting parameter with names


$user=$db->Sign_Up()->where (array('email_id'=>$email, 'password'=> $pass));
$data=$user->fetch();
$status=$data['status'];
$user_pass=$data['password'];
$user_email=$data['email_id'];
$user_name=$data['user_name'];
$uesr_ip=$_SERVER['REMOTE_ADDR'];


if ($pass!=$user_pass || $email != $user_email)
{
echo json_encode( 
array(
'status'=>false,
'Message'=>'Email or Password dose not match.'
));

}
else if($status=='active')
    {
            $user_os        =   getOS();
            $user_browser   =   getBrowser();

$message = "
<div style='font-size:15px;text-align:center;border: 1px solid #ddd;background: #DADADA;margin: 2%;padding: 5%;'>
Hi $user_name,
Your KarAssist Account $user_email was just used to sign in from $user_browser on $user_os.
</div>
";

$headers  = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";



            $mail_sent=mail($user_email, "New Sign In from $user_browser on $user_os", $message,$headers);

            if ($mail_sent)
            {
                        echo json_encode(array(
    'User_Info'=>array(
                        'status'=>true, 
                        'id'=> $data['id'],
                        'First_Name'=>$data['first_name'],
                        'Email_Id'=>$data['email_id'],
                        'Last_Name'=>$data['last_name'],
                        'Phone_Number'=>$data['phone_number'],
                        'Status'=>$data['status']

    ),

    'App_info'=>array(
                    'Device_token'=>$data['device_token'],
                    'App_version'=>$data['app_version']         
        )));

            }
            else
            {
                echo json_encode(array(
    'status'=>false,
    'message'=>"There is some Technical error. We are sorry to inform you we could not sent mail to you. So please change your mail ASAP."
    ));


            }
}
else
{
    echo json_encode(array(
    'status'=>false,
    'message'=>"User with Email $email is not avtivated yet."
    ));
}
});

This code is working good. I want to implement Basic auth with it. So let me know how to do it.

  • 写回答

1条回答

  • dongxue163536 2016-03-01 15:38
    关注

    Easiest thing is to use an existing Basic Auth middleware. With this middleware you can do something like following:

    $app = new \Slim\App;
    
    $app->add(new \Slim\Middleware\HttpBasicAuthentication([
        "path" => "/api",
        "realm" => "Protected",
        "users" => [
            "root" => "t00r",
            "user" => "passw0rd"
        ]
    ]));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!