du7979 2014-08-24 23:11
浏览 52

ios将数据发送到php并从php检索变量

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!!!!

  • 写回答

1条回答 默认 最新

  • dongwu9647 2014-08-24 23:44
    关注

    What is strange? responseData is the data displayed in hex, that is how data is generally displayed, there are many ways to display data. To see the data as a string (if it is) use: NSLog(@"responseData as a string: %@", [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding]);

    Running the hex through a hex to ascii converter on the web produces:

    <br />
    <b>Notice</b>:  Undefined index: password in <b>/var/www/html/music/core/loginS.php</b> on line <b>7</b><br />
    {"success":false,"message":"\u5e10\u53f7\u6216\u8005\u5bc6\u7801\u9519\u8bef"}
    

    Which displayed formatted:


    Notice: Undefined index: password in /var/www/html/music/core/loginS.php on line 7
    {"success":false,"message":"\u5e10\u53f7\u6216\u8005\u5bc6\u7801\u9519\u8bef"}"

    Which is html and explains the problem.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭