I am creating an application that requires users to enter a username and password. once they fill out the textfields the data gets sent to locahost using GET. My php is set to return the value 1 if the username and password match and 2 if they don’t. But if i try to create a if statement like this:if ([received isEqualTo:@"1"]) {
NSLog(@"Access Granted");
}else{
NSLog(@"Access Denied");
}
It returns as access denied even though if i do a Nslog it returns as 1 but it still doesnt say access granted.
This is AppDelage.m:
//
// AppDelegate.m
// possys
//
// Created by on 11/11/12.
// Copyright (c) 2012 . All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize username;
@synthesize password;
@synthesize super;
@synthesize alert;
@synthesize connection = _connection;
@synthesize receivedData = _receivedData;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (IBAction)login:(id)sender {
NSString *user = [username stringValue];
NSString *pass = [password stringValue];
NSString *strurl = [NSString stringWithFormat:@"http://localhost/index.php?username=%@&password=%@",user,pass];
[self.connection cancel];
//initialize new mutable data
NSMutableData *data = [[NSMutableData alloc] init];
self.receivedData = data;
//initialize url that is going to be fetched.
NSURL *url = [NSURL URLWithString:strurl];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//initialize a connection from request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = connection;
//start the connection
[connection start];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"%@" , error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//initialize convert the received data to string with UTF8 encoding
NSMutableString *received = [[NSMutableString alloc] initWithData:self.receivedData
encoding:NSUTF8StringEncoding];
NSLog(@"%@" , received);
if ([received isEqualTo:@"1"]) {
NSLog(@"YUPYUPYUP");
}else{
NSLog(@"YNO");
}
}
This is my php:
<?php
mysql_connect("localhost","root","abc123") or die (mysql_error());
mysql_select_db("authentication") or die (mysql_error());
$username = $_GET['username'];
$password = $_GET['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$sql = "select * from users where username = '$username' and password = '$password'";
$result = mysql_query($sql) or die (mysql_error());
$count = mysql_num_rows($result);
if ($count == 1) {
echo "1";
}
else{
echo "2";
}
?>
Proceeding on the assumption that there’s some sort of white space issue, you might try:
So that has the system parse the text to obtain a number and then compares the number, rather than looking for an exact string match.