Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-13-2011, 09:30 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 1
Danno2717 is on a distinguished road
Default iPhone to PHP to iPhone

Hi, I am trying to create a login system on an iPhone app but am coming across a bit of a problem. I believe that the code I have is sending the the NSString to the PHP but it is not storing and echoing back the strings like I want it to. What am I doing wrong?

I know the php code isn't doing much right now I just want to see that it is storing the values that I send it.

P.S. yeah it's just hard-coded right now so the username and password are a and s

Thanks!

XCode:

NSString *content = @"myusername=a&mypassword=s";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://mywebsite.php"]];
urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[content dataUsingEncoding:NSISOLatin1StringEncoding]];


// generates an autoreleased NSURLConnection
[NSURLConnection connectionWithRequest:request delegate:self];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);

}


PHP:

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

echo "first ". $_POST['myusename']." return";
echo "second ". $_POST['mypassword']." return";
Danno2717 is offline   Reply With Quote
Old 03-13-2011, 10:57 AM   #2 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

You are not actually sending the myusername and mypassword variables to the php file.

your URL should be this:

Code:
NSString *content = @"myusername=a&mypassword=s";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://mywebsite.php%@", content]]];
PS. Please use code tags

[C O D E] & [/C O D E] (remove the spaces).
Quote:
Originally Posted by Danno2717 View Post
Hi, I am trying to create a login system on an iPhone app but am coming across a bit of a problem. I believe that the code I have is sending the the NSString to the PHP but it is not storing and echoing back the strings like I want it to. What am I doing wrong?

I know the php code isn't doing much right now I just want to see that it is storing the values that I send it.

P.S. yeah it's just hard-coded right now so the username and password are a and s

Thanks!

XCode:

NSString *content = @"myusername=a&mypassword=s";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://mywebsite.php"]];
urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[content dataUsingEncoding:NSISOLatin1StringEncoding]];


// generates an autoreleased NSURLConnection
[NSURLConnection connectionWithRequest:request delegate:self];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);

}


PHP:

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

echo "first ". $_POST['myusename']." return";
echo "second ". $_POST['mypassword']." return";
iSDK is offline   Reply With Quote
Old 03-13-2011, 02:00 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default

@iSDK, that would be assuming he uses GET with his request and he is using POST.

@OP, I do not believe you are actually setting the POST variables in your request. I think it is something like [request setPostValue... forKey... ];

You are then able to populate your request with POST values and receive the information. An easier way as well would be to do what iSDK suggested and default to a GET request as you can receive your information through the URL.
Speed is offline   Reply With Quote
Old 03-13-2011, 11:26 PM   #4 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 140
MatthewD is on a distinguished road
Default

Try setting the content type:
Code:
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
MatthewD is offline   Reply With Quote
Old 03-14-2011, 01:30 AM   #5 (permalink)
New to the SDK
 
franzwarning's Avatar
 
Join Date: Jul 2010
Location: Portland, Oregon
Age: 19
Posts: 165
franzwarning is on a distinguished road
Send a message via AIM to franzwarning Send a message via Skype™ to franzwarning
Default

Hi, I recently did the same thing and here is how I got it to work. I would get the string from a UIText Field like this:
Code:
NSString *passwordstring=password.text;	NSString *usernamestring=username.text;
After I had the string I would send it up to the server like this:
Code:
NSString *url = [ NSString stringWithFormat: @"http://test.com/Login.php?user=%@&pass=%@", usernamestring, passwordstring];
	NSString *escapedURL = [url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
	NSString *webpage = [NSString stringWithContentsOfURL:[NSURL URLWithString:escapedURL]];
and retrieve a string back (that was echoed from the PHP script) from the server saying "YES" or "NO". If it was "YES", i would go to the next page. Like this:
Code:
if([webpage isEqualToString: @"YES"]){
//do the funciton [self presentModalViewetc.....];
}
My php for the login system was a mysql database. I got the username and password in the php script by saying
Code:
$username = $_GET['user'];
$password = $_GET['pass'];
The rest you should know, If you have any more questions just ask me.

Cheers,
George
franzwarning is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, login, php, server

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 368
8 members and 360 guests
Absentia, akphyo, apatsufas, BinHex, fredidf, Kirkout, MarkC, mottdog
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:25 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0