在服务器端具有会话管理的应用程序

时间:2021-11-16 16:34:00

I need to create a server based (php) application with iOS client with authintication "login" mechanisim but i dont known about session in iOS. My application needs login to my website (PHP based website) to get data. I dont known a is the session for the iPhone client is keept in the server?

我需要用iOS客户端创建一个基于服务器(php)的应用程序,使用自动“登录”mechanisim,但是我不知道iOS中的会话。我的应用程序需要登录到我的网站(基于PHP的网站)来获取数据。我不知道iPhone客户端会话是否在服务器中?

Details:

细节:

After sending the login request from iPhone client to my web application, I think here a session is created in my webapp (right?), what about my next request? can I access the same session created for the first request.. In another word : can I share data in the web session (in the server) to be accessible in each request sent by iOS client after login?

在将iPhone客户机的登录请求发送到我的web应用程序之后,我认为在我的webapp(对吗?)中创建了一个会话,那么我的下一个请求呢?我可以访问为第一个请求创建的会话吗?换句话说:我可以在web会话(在服务器上)*享数据吗?

1 个解决方案

#1


18  

Sessions are a server-side concept - there is no session on the client side.

会话是一个服务器端概念——在客户端没有会话。

What normally happens is that the server sets a cookie in the response headers after you log in, and that cookie contains the session ID so that on subsequent requests, the server knows that the client is using that session because it matches up the cookie.

通常情况下,服务器会在您登录后的响应头中设置一个cookie,该cookie包含会话ID,以便在后续请求时,服务器知道客户端正在使用该会话,因为它匹配该cookie。

Cookies work automatically on iOS, so you shouldn't have to do anything at all. NSURLRequests have a property HTTPShouldHandleCookies that defaults to YES, so everything should just work by default.

cookie在iOS上是自动工作的,所以你不需要做任何事情。nsurlrequest有一个默认为YES的属性HTTPShouldHandleCookies,所以所有的一切都应该默认工作。

If it's not working (or if you just want to see what's going on), you can gain access to the cookies in iOS using the NSHTTPCookieStorage APIs. Take a look inside the [NSHTTPCookieStorage sharedHTTPCookieStorage], which works a bit like an NSDictionary. That's where the session cookie should be set after you log in.

如果它不工作(或者您只是想看看发生了什么),您可以使用NSHTTPCookieStorage api访问iOS中的cookie。看看[NSHTTPCookieStorage sharedHTTPCookieStorage],它的工作方式有点像NSDictionary。这是您登录后应该设置会话cookie的地方。

#1


18  

Sessions are a server-side concept - there is no session on the client side.

会话是一个服务器端概念——在客户端没有会话。

What normally happens is that the server sets a cookie in the response headers after you log in, and that cookie contains the session ID so that on subsequent requests, the server knows that the client is using that session because it matches up the cookie.

通常情况下,服务器会在您登录后的响应头中设置一个cookie,该cookie包含会话ID,以便在后续请求时,服务器知道客户端正在使用该会话,因为它匹配该cookie。

Cookies work automatically on iOS, so you shouldn't have to do anything at all. NSURLRequests have a property HTTPShouldHandleCookies that defaults to YES, so everything should just work by default.

cookie在iOS上是自动工作的,所以你不需要做任何事情。nsurlrequest有一个默认为YES的属性HTTPShouldHandleCookies,所以所有的一切都应该默认工作。

If it's not working (or if you just want to see what's going on), you can gain access to the cookies in iOS using the NSHTTPCookieStorage APIs. Take a look inside the [NSHTTPCookieStorage sharedHTTPCookieStorage], which works a bit like an NSDictionary. That's where the session cookie should be set after you log in.

如果它不工作(或者您只是想看看发生了什么),您可以使用NSHTTPCookieStorage api访问iOS中的cookie。看看[NSHTTPCookieStorage sharedHTTPCookieStorage],它的工作方式有点像NSDictionary。这是您登录后应该设置会话cookie的地方。