ユーザーがユーザー名とパスワードを入力する必要があるアプリケーションを作成しています。テキストフィールドに入力すると、GETを使用してデータがlocahostに送信されます。私のphpは、ユーザー名とパスワードが一致する場合は値1を返し、一致しない場合は値2を返すように設定されています。しかし、次のようなifステートメントを作成しようとすると:if ([received isEqualTo:@"1"]) {
NSLog(@"Access Granted");
}else{
NSLog(@"Access Denied");
}
Nslogを実行すると、1として返されますが、アクセスが許可されたとは表示されませんが、アクセスが拒否されたとして返されます。
これは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");
}
}
これは私の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";
}
?>