Slim PHP:如何根据用户ID进行POST或PUT?

时间:2019-03-14 18:54:51

标签: php slim

在学生登录系统后,我如何制作编辑API来编辑数据库中的学生详细信息,而无需再次输入其ID?

下面是我的登录路线

$app->post('/login', function ($request, $response) {

    $input = $request->getParsedBody();
    $sql = "SELECT id, password FROM users WHERE id= :id";
    $sth = $this->db->prepare($sql);
    $sth->bindParam("id", $input['id']);
    $sth->execute();
    $user = $sth->fetchObject();

    // verify email address 
    if(!$user) {
    return $this->response->withJson(['error' => true, 'message' => 'NO ID '], 404);
    }
   // Compare the input password and the password from database for a validation
    if (strcmp($input['password'],$user->password)) {
        return $this->response->withJson(['error' => true, 'message' => 'These credentials do not match our records.'], 404)->withHeader('Content-type', 'application/json;charset=utf-8', 404);  
    }

    return $this->response->withJson($input)->withStatus(200)->withHeader('Content-type', 'application/json;charset=utf-8', 200);
});

//这是我之前尝试过但失败的新

$app->put('/address', function ($request, $response) {
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if(empty($_SESSION['user_id'])) {
    return $response->withJson(['error' => true, 'message' => 'Login please'], 403);
}

$input = $request->getParsedBody();
$sql = "INSERT INTO address (id, s_pNumber, s_address, s_pCode, s_city, s_state, s_country) VALUES 
        (:s_pNumber, :s_address, :s_pCode, :s_city, :s_state, :s_country) WHERE 'user_id' = :id;";
$sth = $this->db->prepare($sql);
$sth->bindParam("s_pNumber", $input['s_pNumber']);
$sth->bindParam("s_address", $input['s_address']);
$sth->bindParam("s_pCode", $input['s_pCode']);
$sth->bindParam("s_city", $input['s_city']);
$sth->bindParam("s_state", $input['s_state']);
$sth->bindParam("s_country", $input['s_country']);
$sth->execute();

return $response->withJson($input)->withStatus(200)->withHeader('Content-type', 'application/json;charset=utf-8', 200);

});

我应该怎么做?即使我已经尝试将session_destory()进行测试,但始终会返回true

1 个答案:

答案 0 :(得分:0)

切勿在生产中使用普通密码。但这不是您要的。.如果您用用户名登录,则可以在SESSION中保存此信息,例如$_SESSION['logged_id'] = $input['id']

在下一个请求中,您可以选中此会话以使用ID登录用户。

$app->post('/login', function ($request, $response) {

    $input = $request->getParsedBody();
    $sql   = "SELECT id, password FROM users WHERE id= :id";
    $sth   = $this->db->prepare($sql);
    $sth->bindParam("id", $input['id']);
    $sth->execute();
    $user = $sth->fetchObject();

    // verify email address
    if (!$user) {
        return $this->response->withJson(['error' => true, 'message' => 'NO ID '], 404);
    }

    // Compare the input password and the password from database for a validation
    if (strcmp($input['password'], $user->password)) {
        return $this->response->withJson(['error' => true, 'message' => 'These credentials do not match our records.'], 404)->withHeader('Content-type', 'application/json;charset=utf-8', 404);
    }

    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }

    $_SESSION['user_id'] = $input['id'];

    return $this->response->withJson($input)->withStatus(200)->withHeader('Content-type', 'application/json;charset=utf-8', 200);
});

$app->get('/my-route-only-for-logged-users', function ($request, $response) {
    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }

    if (empty($_SESSION['user_id'])) {
        return $response->withJson(['error' => true, 'message' => 'Login please'], 403);
    }

    // Now you can get information for certain user, because you know his ID from session...

    return $response
        ->withJson('{"user_id": ' . $_SESSION['user_id'] . '}')
        ->withStatus(200)
        ->withHeader('Content-type', 'application/json;charset=utf-8', 200);
});
相关问题