I am pretty new for ios development. I used to be a web developer. I want to send username and password to my php server,then my php server will send back some feedback. My server works great for web. I searched couple answers but none of them works.
Here is what I have so far:
NSString *rawStr =[NSString stringWithFormat: @"&username=%@&password=%@",_name.text,_pass.text]; //(do I need a question mark before username?)
NSData *data=[rawStr dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[data length]];
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init] ;
request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ec2********/loginS.php"]]]; //the php server is on amazon ec2
[request setHTTPMethod:@"POST"];
[request setHTTPBody: data];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData);
Here is my php server:
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $mysqli->prepare("SELECT COUNT(*), password FROM user WHERE email=?");
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($cnt, $pwd_hash);
$stmt->fetch();
if($cnt == 1 && crypt($password, $pwd_hash)==$pwd_hash){
session_start();
ini_set('session.cookie_httponly',1);
$_SESSION['token'] = substr(md5(rand()), 0, 10);
$_SESSION['email']=$username;
echo json_encode(array(
'success' => true,
'username' => htmlentities($_SESSION['email']),
'token' => htmlentities($_SESSION['token'])
));
exit;
}else{
echo json_encode(array(
'success' => false,
'message' => "oop"
));
exit;
}
?>
I am pretty sure my php server works fine, since my webpage works great with the php server. my console prints some strange things
responseData: <3c627220 2f3e0a3c 623e4e6f 74696365 3c2f623e 3a202055 6e646566 696e6564 20696e64 65783a20 70617373 776f7264 20696e20 3c623e2f 7661722f 7777772f 68746d6c 2f6d7573 69632f63 6f72652f 6c6f6769 6e532e70 68703c2f 623e206f 6e206c69 6e65203c 623e373c 2f623e3c 6272202f 3e0a7b22 73756363 65737322 3a66616c 73652c22 6d657373 61676522 3a225c75 35653130 5c753533 66375c75 36323136 5c753830 30355c75 35626336 5c753738 30315c75 39353139 5c753862 6566227d>
Can anyone show me how to correctly send the data to a php server.
Thanks!!!!