douhaodang0403 2016-01-28 18:29
浏览 50
已采纳

如何从Slim API中的mysql内部的请求中注入数据

I have a Slim API and I need to make a function that will validate a username/password. I'm using a POST and I would like to inject the username inside the credentials inside the sql. I know how to do it if I use GET, but I'm using POST. How can I do that:

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
}

I call this function in a AngularJS service with:

$http.post(appConfig.apiURL + '/authenticate', credentials)
  • 写回答

2条回答 默认 最新

  • douqian2957 2016-01-29 16:27
    关注

    I figured it out:

    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() .'}}';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面