How to store a $_SESSION, Objective-C

1.1k views Asked by At

Is there any way to catch and store a $_SESSION in Objective-C at all? I know that both session and cookies is extremely intertwined, so is it possible to use NSHTTPCookie to catch it or something? Or is this something i have to handle in the php code and not in my objective-c code to make this work?

I can tell you what i'm trying to do. I'm trying to send some information to a server trough NSMutableURLRequest which is not a problem, and then i'm trying to catch the session that i then would be recieving.

1

There are 1 answers

0
Joachim Isaksson On BEST ANSWER

Sessions are stored on the server side, so no data that is stored in the session is available to the client unless the server chooses to send it in some other way. The only thing that is passed to the client and back is a session identifier, which the server uses to locate the correct session when the client sends a request.

The session identifier is usually passed in a cookie, but the name of the cookie will vary by server. PHP often uses PHPSESSID, but it's configurable to any name in php.ini.

To get all the cookies passed from a server to your app (which will give you the session cookie if it has been set) is to do;

NSHTTPCookieStorage *cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [cookieStore cookiesForURL:url];

...passing in an the NSURL object pointing to the server. This will return an NSArray of NSHTTPCookie, which you can loop through and get the information you want.