2014年6月23日 星期一

在file_get_contents中,保留session

設定完cookie即可
session_start();
$context = stream_context_create(array(
'http' => array(
 'method' => 'POST',
 'header' => "Content-Type: application/json\r\n".
 'Cookie: '.session_name().'='.session_id()."\r\n"
)));
session_write_close();

在iOS連線中,保留session

只需要將cookie存在cookieStorage裡即可
- (NSData *)send:(NSHTTPURLResponse**)urlResponse
{
    NSError *errorState = nil;
    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:urlResponse error:&errorState];
    
    // process cookie
    NSArray *allCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[*urlResponse allHeaderFields] forURL:[*urlResponse URL]];
    if ([allCookies count]) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:allCookies forURL:[*urlResponse URL] mainDocumentURL:nil];
    }

    return result;
}