Skip to content
Kieron Howard
  • Home
  • About
  • Blog
Book a Consultation
Kieron Howard
  • Home
  • About
  • Blog
Kieron Howard

iPhone Login App using PHP MySQL Tutorial

  • Kieron HowardKieron Howard
  • April 16, 2010
  • Development

I needed an easy authentication for my first iPhone app. There didnt seem to be any info on the internet about using PHP/MySQL, so Ive posted this. Im not sure if Apple would even allow it onto the appstore, as I have no plans to submit the app. The code is probably very messy and inefficient as it was my first attempt at Objective-C.

Ill assume you know how to set up a MySQL Database and connect to it using PHP.

Your PHP should look something like:

<?
session_start();
require(“iphoneconnection.php”);

$u = $_GET[‘username’];
$pw = $_GET[‘password’];

$check = “SELECT username, password FROM iphoneusers WHERE username=’$u’ AND password= ‘$pw’”;

$login = mysql_query($check, $connect) or die(mysql_error());

if (mysql_num_rows($login) == 1) {
$row = mysql_fetch_assoc($login);
echo ‘Yes’;exit;

} else {

echo ‘No’;exit;

}

mysql_close($connect);

?>

Where iPhoneconnection.php contains all your database connection info.

Onto the iPhone side.

I reccommend you follow this guide for creating the interface, its better written than my efforts:

http://www.riccomini.name/Topics/Mobile/iPhone/SimpleLoginScreen/

1. Open XCode, create a new project. Use a View Based Application. Ive called mine Login.

grab1

In your LoginViewControler.h put:

#import <UIKit/UIKit.h>

@interface loginViewController : UIViewController {
    IBOutlet UITextField *usernameField;
    IBOutlet UITextField *passwordField;
    IBOutlet UIButton *loginButton;

    
}

@property (nonatomic, retain) UITextField *usernameField;
@property (nonatomic, retain) UITextField *passwordField;
@property (nonatomic, retain) UIButton *loginButton;

– (IBAction) login: (id) sender;

@end

This initialises the UI elements such as the Username and Login Button which we will be creating in IB in a min.

In your LoginViewController.m put:

#import “loginViewController.h”

@implementation loginViewController

@synthesize usernameField;
@synthesize passwordField;
@synthesize loginButton;

– (IBAction) login: (id) sender

{
    
    
    
    NSString *post =[NSString stringWithFormat:@”username=%@&password=%@”,usernameField.text, passwordField.text];
    
    NSString *hostStr = @”THIS IS MY URL/iphonelogin-do.php?”;
    hostStr = [hostStr stringByAppendingString:post];
    NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];    
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];

    
    
    
    
    
    
    if([serverOutput isEqualToString:@”Yes”]){
        
                
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@”Congrats” message:@”You are authorized “
                                                              delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil, nil];
                
        [alertsuccess show];
        [alertsuccess release];
            
                    
    } else {
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@”Error” message:@”Username or Password Incorrect”
                                                              delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil, nil];
        [alertsuccess show];
        [alertsuccess release];
        
    }
    
}

– (void)didReceiveMemoryWarning {
    // Releases the view if it doesn’t have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren’t in use.
}

– (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

– (void)dealloc {
    [super dealloc];
}

@end

To explain:

This part :

NSString *post =[NSString stringWithFormat:@”username=%@&password=%@”,usernameField.text, passwordField.text];
    
    NSString *hostStr = @”THIS IS MY URL/iphonelogin-do.php?”;
    hostStr = [hostStr stringByAppendingString:post];
    NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];    
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];

Adds the data entered into the username and password field to the end of the iphonelogin-do.php? as a POST, which is then checked in the PHP. It then returns a “Yes” or “No” which is checked here:

    if([serverOutput isEqualToString:@”Yes”]){

Obviously you’ll need to change “This is my URL” to your own.

Next:

grab3

3. Open up Interface Builder

grab4

Create 2 text fields and a button. Link your 2 fields  to the actions by alt clicking and drag to the Files Owner.

files owner

Do the same for the Login button:

grab5

Now run the app and you should get an error message for a incorrect entry:

grab6

And success for a correct one:

lastgrab

Boom.

Source is now available here:

http://www.megaupload.com/?d=FNC0716V

UPDATE:

re-uploaded source:

http://rapidshare.com/files/453292790/Kiksy-iphone-login.zip

http://www.megaupload.com/?d=ETG0VHSN

Share this:

  • Email a link to a friend (Opens in new window) Email
  • Tweet

Like this:

Like Loading...
Share your love
Kieron Howard
Kieron Howard
Articles: 53
Previous Post Building Hackingtosh OSX86 using GIGABYTE GA-EP45-UD3P Intel P45 ATX Intel Motherboard
Next Post Sunrise Timelapse in the Maldives

Leave a ReplyCancel Reply

Your email address will not be published. Required fields are marked *

Subscribe now!

Enter your email address below and subscribe to our newsletter

Initial look at the Laravel PHP framework

March 23, 2013

Reverse engineering Quark Shockwiz – Part 1

May 12, 2017

How to upload pre-made videos to Instagram

June 25, 2013

Vagrant on Windows – Improve slow performance by using SMB instead of NFS

January 30, 2014

Bash on Ubuntu on Windows – SSH using private key and Windows permissions

June 28, 2018

“Beware of little expenses, a small leak will sink a great ship”

— Benjamin Franklin

Contact Info

  • Email: contact@kieronhoward.co.uk

Copyright © 2026 - WordPress Theme by CreativeThemes
%d