如何使用rest api post验证会话

时间:2015-06-11 02:54:35

标签: php codeigniter api session

我使用会话进行API登录,当移动应用程序使用登录功能时,实际上他们会点击API。在API登录中,我进行了会话登录,因此当用户登录时会给出响应会话。检查下面的代码:

  public function user_post()
  {
    $data = array (
      'username' => $this->input->get_post('username'),
      'password' => sha1($this->input->get_post('password'))
    );

    $result = $this->login_m->user_check($data);
    if ($result ) {
       foreach ($result as $row ) {

        $sess_array = array(
                    'username' => $row->username,
                    'email'    => $row->email
                    );

        $this->session->set_userdata('logged', $sess_array);
        $this->response(array('success' => $this->session->userdata('logged') ));
      }
    } else {
      $this->response(array(404 => 'missing parameter'));
    }
  }

,响应将如下所示:

  * {
  *  "success":
  *  {
  *    "username": "johndoe123",
  *    "email": "myemail@my.com"
  *   }
  * }

我的问题是,如何让会话验证API帖子?例如:

我发布了用于存储新数据的API。我想这种方式会很好,设置param用codeigniter捕获会话名'logged',在会话'logged'已经有电子邮件和用户名,所以会用它作为条件检查到表是电子邮件,用户名在表格中。

$this->session->has_userdata('logged')

因此,移动应用需要将会话保存在他们的应用中,以便再次作为参数发送。代码如下所示:

$data = array(
            'idcardno'        => $this->input->get_post('idcardno'),
            'dateofbirth'       => $this->input->get_post('dateofbirth')
);

$addnewpolis =  $this->modelname->modelmethod($data2);
谢谢你们,

CMIIW

1 个答案:

答案 0 :(得分:1)

您不能在代码中使用外部api调用来使用您想要的会话。您可以从登录中生成令牌并将其返回。然后,在您的移动设备的下一次api呼叫中,发送此令牌以了解用户身份。

为什么:Is it good to implement REST api using Sessions?

生成令牌: https://www.google.com/search?q=generate%20token%20php&rct=j

然后在您的回复中将其保存并保存到某个地方,以便在下次调用时检索它。