Hello

I am trying to write a test program that will send a value through a web form, get the cookie information and then relay that cookie information back to the sever. I have setup 3 php pages, which work correctly. The first page displays a web form with one field named "name". The second page "validates" the input, puts the "name" variable into a session variable, then forwards you to the third page. The third page checks for the session variable and then either displays "You are logged in as: Bob" (using the $name / session variable) or displays "failure". Each page also displays the SessionID at the top of the screen. When I run these PHP pages through a browser, it works as expected. When I try to run this through Cocoa, I am able to retrieve the Session Cookie, but when I call the data from the third page, I always get a "failure" response, even though it seems to be relaying the correct Session ID back to the server.

I have found many different articles online, but they all seem to be giving the same basic code, which doesn't seem to work. I've tried several different variations but I'm not sure where to go from here.

You can access the form at http://www.abovetheclamor.com/testform.php

Here are all the files I'm using:





### PHP 1 (testform.php) ###
<?PHP
session_start();
echo 'SESSIONID: ', session_id(), '<br>';
echo '<form method="POST" action="testform2.php">
<input type="text" name="name" size="20"><br>
<input type="submit">
</form>';
?>





### PHP 2 (testform2.php) ###
<?PHP
session_start();
echo 'SESSIONID: ', session_id(), '<br>';
$_SESSION['sessionvariable'] = $_POST['name'];
header("Location: testform3.php");
?>






### PHP 3 (testform3.php) ###
<?PHP
session_start();
echo 'SESSIONID: ', session_id(), '<br>';
if ($_SESSION['sessionvariable'] == NULL) {
        echo 'failure on page 3';
        exit;


} else {

        echo 'You are logged in as: ';
        echo $_SESSION['sessionvariable'];

}

?>




### Cocoa / Objective-C ###
- (void)applicationDidFinishLaunching:(UIApplication *)application {
        NSHTTPURLResponse *response;
        NSError *error;
NSString *post = @"name=Bob"; // send the value "Bob" to the form NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

  // setup the request to submit the data to the form
[request setURL:[NSURL URLWithString:@"http://www.abovetheclamor.com/testform.php "]];
        [request setHTTPMethod:@"POST"]; // run it as a POST
[request setValue:postLength forHTTPHeaderField:@"Content- Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        // run the request
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

  // run the request to get the cookies
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

  // get allCookies from the request
NSArray *allCookies = [NSHTTPCookie cookiesWithResponseHeaderFields: [response allHeaderFields]

forURL:[NSURL URLWithString:@"http://www.abovetheclamor.com/testform.php "]];

        NSLog(@"How many Cookies: %d", allCookies.count);

        for (NSHTTPCookie *cookie in allCookies) {
NSLog(@"Name: %@ : Value: %@, Expires: %@", cookie.name, cookie.value, ookie.expiresDate);
        }

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:allCookies

forURL:[NSURL URLWithString:@"http://www.abovetheclamor.com/";] mainDocumentURL:nil];


NSArray * availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"http://www.abovetheclamor.com/ "]];

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies];
        NSMutableURLRequest *req = [[NSMutableURLRequest alloc] init];

        // set the request to call the final page
[req setURL:[NSURL URLWithString:@"http://www.abovetheclamor.com/ testform3.php"]];
        [req setAllHTTPHeaderFields:headers];
        [req setHTTPShouldHandleCookies:NO];

        // send the request to the final page
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];

        // what did the server get? probably "failure". why?
NSLog(@"The server saw: %@", [[[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding] autorelease]);
    [window makeKeyAndVisible];


}




Any help or guidance that you can provide would be greatly appreciated
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to