Flutter http维护PHP会话 [英] Flutter http Maintain PHP session

查看:136
本文介绍了Flutter http维护PHP会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的人.基本上,我在Web应用程序中使用代码Igniter框架.我为我的Web应用程序创建了REST API,在用户使用API​​登录后,所有方法都会检查session_id是否存在,然后继续,如果不存在,则给出

I'm new to flutter. Basically I'm using code Igniter framework for my web application. I created REST API for my web app, after user login using API all the methods check for the session_id if it exists then it proceeds, and if it doesn't then it gives

{ ['status'] = false, ['message'] = 'unauthorized access' }

我正在使用flutter创建应用程序,当我使用flutter的http方法时,它会更改每个请求的会话.我的意思是,它不维护会话.我认为它每次都会破坏并创建新的连接.这是我用于api调用get和post请求的thr类方法.

I'm creating app with flutter, when i use the http method of flutter it changes session on each request. I mean, it doesn't maintain the session. I think it destroys and creates new connection each time. Here is thr class method which i use for api calls get and post request.

class ApiCall {  
  static Map data;
  static List keys;

static Future<Map> getData(url) async {
 http.Response response = await http.get(url);
 Map  body =  JSON.decode(response.body);
 data = body;
 return body;
}

static Future postData(url, data) async {
Map result;    
http.Response response = await http.post(url, body: data).then((response) {
  result = JSON.decode(response.body);
}).catchError((error) => print(error.toString()));

data = result;
keys = result.keys.toList();

return result;  
}

我想发出API请求,然后存储session_id, 并可以在服务器上维护会话,以便我可以自行管理Web应用程序上的身份验证.?

I want to make API request and then store session_id, And is it possible to maintain session on the server so i can manage authentication on the web app it self.?

推荐答案

HTTP是无状态协议,因此服务器需要某种方式来识别对服务器的第二,第三和后续请求中的客户端.在您的情况下,您可以使用第一个请求进行身份验证,因此您希望服务器在后续请求中记住您,以便服务器知道您已通过身份验证.常见的方法是使用 Cookie .

HTTP is a stateless protocol, so servers need some way to identify clients on the second, third and subsequent requests they make to the server. In your case you might authenticate using the first request, so you want the server to remember you on subsequent requests, so that it knows you are already authenticated. A common way to do this is with cookies.

点火器发送一个带有会话ID的cookie.您需要从每个响应中收集此信息,然后在下一个请求中将其发送回去. (服务器有时会更改会话ID(以减少我们不需要考虑的诸如clickjacking之类的事情),因此您需要继续从每个响应中提取cookie.)

Igniter sends a cookie with the session id. You need to gather this from each response and send it back in the next request. (Servers sometimes change the session id (to reduce things like clickjacking that we don't need to consider yet), so you need to keep extracting the cookie from every response.)

cookie作为称为set-cookie的HTTP响应标头到达(可能不止一个,尽管希望不是为了简单).要发回Cookie,请向后续的称为cookie的请求中添加HTTP请求标头,并复制从set-cookie标头中提取的某些信息.

The cookie arrives as an HTTP response header called set-cookie (there may be more than one, though hopefully not for simplicity). To send the cookie back you add a HTTP request header to your subsequent requests called cookie, copying across some of the information you extracted from the set-cookie header.

希望,Igniter仅发送一个set-cookie标头,但出于调试目的,您可能会发现使用response.headers.forEach((a, b) => print('$a: $b'));打印所有标头很有用.您应该找到Set-Cookie: somename=abcdef; optional stuff.我们需要提取字符串,直到但不包括;,即somename=abcdef

Hopefully, Igniter only sends one set-cookie header, but for debugging purposes you may find it useful to print them all by using response.headers.forEach((a, b) => print('$a: $b'));. You should find Set-Cookie: somename=abcdef; optional stuff. We need to extract the string up to, but excluding the ;, i.e. somename=abcdef

在下一个及后续请求上,通过将post命令更改为以下内容,将请求标头添加到下一个{'Cookie': 'somename=abcdef'}请求中:

On the next, and subsequent requests, add a request header to your next request of {'Cookie': 'somename=abcdef'}, by changing your post command to:

http.post(url, body: data, headers:{'Cookie': cookie})

顺便说一句,我认为您在上面的代码中awaitsthen不匹配.通常,如果类应该是顶级函数,则不希望使用类中的静态函数.相反,您可以创建一个cookie感知类,例如:

Incidentally, I think you have a mismatch of awaits and thens in your code above. Generally, you don't want statics in classes, if they should be top level functions instead. Instead you could create a cookie aware class like:

class Session {
  Map<String, String> headers = {};

  Future<Map> get(String url) async {
    http.Response response = await http.get(url, headers: headers);
    updateCookie(response);
    return json.decode(response.body);
  }

  Future<Map> post(String url, dynamic data) async {
    http.Response response = await http.post(url, body: data, headers: headers);
    updateCookie(response);
    return json.decode(response.body);
  }

  void updateCookie(http.Response response) {
    String rawCookie = response.headers['set-cookie'];
    if (rawCookie != null) {
      int index = rawCookie.indexOf(';');
      headers['cookie'] =
          (index == -1) ? rawCookie : rawCookie.substring(0, index);
    }
  }
}

这篇关于Flutter http维护PHP会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆