我正在创建一个要求用户输入用户名和密码的应用程序。一旦他们填写了文本字段,数据就会使用 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";
}
?>