ASIFormDataRequest submitting POST command to login with .php

84 views
Skip to first unread message

TTGator

unread,
Jun 2, 2011, 9:29:31 PM6/2/11
to ASIHTTPRequest
Hey guys, I'm brand new to iOS development, and I'm trying to create
an app that will be based off of a website that a friend of mine has
already created. There is a login.php file that handles login and
keeping track of the session. I'm trying to use an ASIFormDataRequest
to submit a POST to the login script that just needs the username and
password. But after a day of Googling, I can't figure out what is
wrong. The 'action' is nothing special, just another parameter
right? Tried a bunch of stuff and failed. Here's a snippet of
the .php and my Objective-C code.

.php
------

<?php
session_start();
$intPageUsageID = 0;

include_once("Application.php");
include_once("$GlobalIncPath/dbconn.php");

// If this page is being called as a login, process the login
if ($_POST['action']=="login") {
include_once("$GlobalIncPath/class.user.php");
// Construct new User object
$UserObject = new User($db, $ErrorMsg);
$UserLogin = $UserObject->UserLogin($_POST['username'],
$_POST['password']);

if (empty($ErrorMsg)){// Login was successful so load he lunch
details for today
include_once("$GlobalIncPath/class.lunch.php");
// Construct new Lunch object
$LunchObject = new Lunch($db, $ErrorMsg);
$LunchDetail = $LunchObject->LunchDetail($UserLogin['UserID']);
}

// Set session variables after logging in
$_SESSION['intUserID'] = $UserLogin['UserID'];
$_SESSION['intAdmin'] = $UserLogin['Admin'];
$_SESSION['strDefZip'] = $UserLogin['DefaultZip'];
$_SESSION['numDefLat'] = $UserLogin['Lat'];
$_SESSION['numDefLon'] = $UserLogin['Lon'];

if (isset($_SESSION['intUserID'])){
$_SESSION['strUsername'] = $_POST['username'];
}

if (!empty($_POST['fpg'])||!empty($_GET['fpg'])) {
$fpg = urldecode($_POST['fpg']).urldecode($_GET['fpg']);
header("location:".$fpg); // Redirect
exit;
}
header("location:index.php"); // Redirect
}

// If this page is being called as a logout, redirect to logout.php
if ($_POST['action']=="logout") {
header("location:Logout.php"); // Redirect
}


code
------


NSURL *url = [NSURL URLWithString:@"http://myurl.com"];

ASIFormDataRequest *request = [ASIFormDataRequest
requestWithURL:url];
[request setPostValue:@"login" forKey:@"action"];
[request setPostValue:usernameField.text forKey:@"username"];
[request setPostValue:passwordField.text forKey:@"password"];
// [request setPassword:passwordField.text];
// [request setUsername:usernameField.text];

NSLog(@"Request data: %@", request);

[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(@"response = %@", response);
}
else {
NSLog(@"ERROR = %@", [error localizedFailureReason]);
}

Joseph Heenan

unread,
Jun 3, 2011, 6:01:04 AM6/3/11
to asihttp...@googlegroups.com
On 03/06/2011 02:29, TTGator wrote:
> Hey guys, I'm brand new to iOS development, and I'm trying to create
> an app that will be based off of a website that a friend of mine has
> already created. There is a login.php file that handles login and
> keeping track of the session. I'm trying to use an ASIFormDataRequest
> to submit a POST to the login script that just needs the username and
> password. But after a day of Googling, I can't figure out what is
> wrong. The 'action' is nothing special, just another parameter
> right? Tried a bunch of stuff and failed. Here's a snippet of
> the .php and my Objective-C code.
>
<...>

> NSURL *url = [NSURL URLWithString:@"http://myurl.com"];
I presume this did have a URL ending login.php and you've just removed
the server details before posting the code to the list?

Other than that, I can't see anything obviously wrong.

What actually happens? ie. do you get an error or a response?

Try using charlesproxy or wireshark to capture a successful login with a
browser and an unsuccessful login from the iOS emulator.

Cheers,

Joseph

TTGator

unread,
Jun 3, 2011, 3:57:08 PM6/3/11
to ASIHTTPRequest


On Jun 3, 6:01 am, Joseph Heenan <jos...@heenan.me.uk> wrote:
>
> I presume this did have a URL ending login.php and you've just removed
> the server details before posting the code to the list?
>
> Other than that, I can't see anything obviously wrong.
>
> What actually happens? ie. do you get an error or a response?
>
> Try using charlesproxy or wireshark to capture a successful login with a
> browser and an unsuccessful login from the iOS emulator.
>
> Cheers,
>
> Joseph

I figured it out. I had been all around the solution but didn't quite
get there until late last night. The above code is missing a the line
[request setPostValue:@"" forKey:@"fpg"];
which I noticed right before downloading and trying Charlesproxy,
which I had seen somewhere else. Great tool, btw.

But the problem [embarrassingly] was that the login.php part of the
URL that you mention is CASE SENSITIVE. I had been trying the URL as
NSURL *url = [NSURL URLWithString:@"http://myurl.com/login.php"];
That returned me a 404 page though. So I figured it must not need to
go on the URL, when in fact it actually needed to be myurl.com/
Login.php! Ugh. Since when are URL's case sensitive?

Anyway, it's working now after so much frustration, and learning new
tools, it was great to see the correct data finally come back!

-Aaron
Reply all
Reply to author
Forward
0 new messages