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 08-25-2011, 11:37 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 27
rappa819 is on a distinguished road
Default Question about iPhone and $_GET

Ok, so I have an application that takes a name and email and saves them in a database via URL GET method. This was the only way I could figure it out from the tutorials I read on google. But I noticed if you just go to the page on my server with the php code, it just inserts a blank row into the database.

How can I avoid this?

Also, can anyone critique my code to make it more secure?

Code:
- (IBAction)saveUsername:(UIButton *)sender {    
    if ([self checkFields]) {
        //Show activity indicator
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        [activityIndicator startAnimating];
        
        //Save username
        NSUserDefaults *applicationDefaults = [NSUserDefaults standardUserDefaults];
        //Save the device ID, may be useful to use as ID for 'accounts'
        UIDevice *myDevice = [UIDevice currentDevice];
        NSString *deviceUDID = [myDevice uniqueIdentifier];
        
        //Save in database
        NSString *post =[NSString stringWithFormat:@"un=%@&em=%@&UDID=%@",username.text, email.text,deviceUDID];
        NSString *hostStr = @"http://www.site.net/adduser.php?";
        hostStr = [hostStr stringByAppendingString:post];
        NSData *dataURL =  [NSData dataWithContentsOfURL: [NSURL URLWithString: hostStr]];    
        NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
        
        if([serverOutput isEqualToString:@"Yes"]){
            //Save in XML
            [applicationDefaults setObject:username.text forKey:@"Username"];
            [applicationDefaults setObject:email.text forKey:@"UserEmail"];
            [applicationDefaults setObject:deviceUDID forKey:@"DeviceUDID"];
            //Save
            [applicationDefaults synchronize];
            
            //Debugging
            NSLog(@"User Added");
            
            //Hide activity indicator
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            [activityIndicator stopAnimating];
            
            //Dismiss
            [self dismissModalViewControllerAnimated:YES];
        } 
        else {
            //Network error or user exists
            NSLog(@"User Not Added");
            
            UIAlertView *networkError = [[UIAlertView alloc] initWithTitle:@"Uh Oh!" message:@"Seems you may be having network problems, please connect to a network and try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [networkError show];
            [networkError release];
        }
    }
}
adduser.php

PHP Code:
<?
$con 
mysql_connect("localhost","..","..");
if (!
$con) {
    die(
'Could not connect: ' mysql_error());
}

mysql_select_db("_master"$con);

//Grab username, email, and device ID from URL
$u $_GET['un'];
$e $_GET['em'];
$udid $_GET['UDID'];

//Put it in the database
$query mysql_query("INSERT INTO users (Username, Email, UDID) VALUES ('$u', '$e', '$udid')");

//Check to see if its there
$search mysql_query("SELECT UDID FROM users WHERE UDID='$udid'");

if (
mysql_num_rows($search) == 1) {
    echo 
'Yes';
    exit;
} else {
    echo 
'No';
    exit;
}

mysql_close($con);
?>
Thanks in advance
rappa819 is offline   Reply With Quote
Old 08-26-2011, 03:06 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

So create a web service instead... or maybe just put logic into your PHP code to not create a record when there is no data to insert.

Also, now is probably not the best time to create an app that grabs the device UDID. Considering all the recent discussion on tech sites about that particular API.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 08-26-2011, 07:26 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 73
Thunderscreech is on a distinguished road
Default

I think the base bug is that you're not starting 'post' with a question mark. A GET URL is http://site.com?variable1=data&variable2=data etc. You just appended the data stuff to hte host unless I missed something (which is possible, I just woke up).

As for improvements, might I suggest adding some error checking? Your PHP just blindly runs with the assumption that it could only be called by the iphone app. It should be validating the integrity of the data before posting it to a database. Is there an email address? Is there a UDID? etc, it's just firing blind.

BTW, it's usually normal to use POST for operations that result in data changes. GET is typically reserved for queries, but this isn't probably a huge deal in this case if you're adding data to rows instead of doing other operations. I remember hearing of Google's spider accidentally destroying some websites that had data-changing links that used the GET method, for instance.
Thunderscreech is offline   Reply With Quote
Old 08-26-2011, 08:18 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 27
rappa819 is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
So create a web service instead... or maybe just put logic into your PHP code to not create a record when there is no data to insert.

Also, now is probably not the best time to create an app that grabs the device UDID. Considering all the recent discussion on tech sites about that particular API.
I've heard about web services but i've never actually seen a tutorial on one. Guess i'll start searching. Also, how could I check in my php to only allow queries from mobile devices?

Like I said, this was the only way I could figure out how to do this, there will be a lot more database implementation going on so I want to make sure its secure and up to date with the current ways.
rappa819 is offline   Reply With Quote
Old 08-26-2011, 08:28 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 27
rappa819 is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
So create a web service instead... or maybe just put logic into your PHP code to not create a record when there is no data to insert.

Also, now is probably not the best time to create an app that grabs the device UDID. Considering all the recent discussion on tech sites about that particular API.
Also, whats wrong with the UDID?
rappa819 is offline   Reply With Quote
Old 08-26-2011, 08:30 AM   #6 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 73
Thunderscreech is on a distinguished road
Default

BTW, you're not doing any string safing on you $u, $e, and $udid. You insert them directly into your SQL statement and they could contain breakout characters to run arbitrary commands against your database.

Use mysql_real_escape_string() to safe those strings up, and be sure to limit the rights on the user your PHP script is using to connect to the database.
Thunderscreech is offline   Reply With Quote
Old 08-26-2011, 09:26 AM   #7 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 27
rappa819 is on a distinguished road
Default

Quote:
Originally Posted by Thunderscreech View Post
BTW, you're not doing any string safing on you $u, $e, and $udid. You insert them directly into your SQL statement and they could contain breakout characters to run arbitrary commands against your database.

Use mysql_real_escape_string() to safe those strings up, and be sure to limit the rights on the user your PHP script is using to connect to the database.
Yes thank you I just did that.

I just installed ASIHTTPRequest into my application. It seems to be documented very well, would this be a more appropriate solution for what i am trying to accomplish?
rappa819 is offline   Reply With Quote
Old 08-26-2011, 09:40 AM   #8 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Quote:
Originally Posted by rappa819 View Post
Also, whats wrong with the UDID?
Apple is deprecating the UDID for privacy and other reasons.
baja_yu is offline   Reply With Quote
Old 08-26-2011, 10:54 AM   #9 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 73
Thunderscreech is on a distinguished road
Default

Quote:
Originally Posted by rappa819 View Post
Yes thank you I just did that.

I just installed ASIHTTPRequest into my application. It seems to be documented very well, would this be a more appropriate solution for what i am trying to accomplish?
I've used ASIHTTPRequest to POST, it was very straightforward.
Thunderscreech is offline   Reply With Quote
Old 08-26-2011, 05:28 PM   #10 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Quote:
Originally Posted by rappa819 View Post
Also, whats wrong with the UDID?
Quote:
Originally Posted by baja_yu View Post
Apple is deprecating the UDID for privacy and other reasons.
Exactly.

You can still easily create your own unique ID (via CFUUIDCreate); it just won't be tied to the device (it'll be tied to the pairing of the device and your app, or the user and your app, depending on how you do it and depending on if the user uses multiple devices or simply upgrades to a new device periodically by restoring from a backup of the previous device). But it would create unnecessary headaches for you if you start off by using the UDID, and then have to try to migrate that somehow down the road.

Quote:
Originally Posted by rappa819 View Post
Yes thank you I just did that.

I just installed ASIHTTPRequest into my application. It seems to be documented very well, would this be a more appropriate solution for what i am trying to accomplish?
Quote:
Originally Posted by Thunderscreech View Post
I've used ASIHTTPRequest to POST, it was very straightforward.
+1.

ASIHTTPRequest is a very robust library. Definitely the way to go for, I'd say, 98% of iOS apps needing to do any HTTP communication.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 08-27-2011, 02:53 AM   #11 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

However here an alternative to udid in order to retrieve an unique number for device for our app (it combine mac address with bundle)

https://github.com/gekitz/UIDevice-w...fier-for-iOS-5
__________________
dany_dev is offline   Reply With Quote
Old 08-27-2011, 09:22 PM   #12 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 27
rappa819 is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
However here an alternative to udid in order to retrieve an unique number for device for our app (it combine mac address with bundle)

https://github.com/gekitz/UIDevice-w...fier-for-iOS-5
This is cool, thanks
rappa819 is offline   Reply With Quote
Reply

Bookmarks

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: 390
16 members and 374 guests
7twenty7, chiataytuday, cristofercolmbos, dedeys78, fiftysixty, gmarro, iOS.Lover, jonathandeknudt, kilobytedump, Matrix23, raymng, ryantcb, stanny, tymex, UMAD, xerohuang
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

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