objective c - Authenticating on php script from iOS/iphone application -
this php need authenticate:
<?php $username=$_post["m_username"]; $password=$_post["m_password"]; /* $username=$_get["m_username"]; $password=$_get["m_password"]; */ ?>
i using below code authenticate iphone app. not authenticating. dont know issue not working. says null parameters/values sent.
nsstring *urlasstring =[nsstring stringwithformat:@"http://www.myurl.com/abc/authenticate.php"]; nsurl *url = [nsurl urlwithstring:urlasstring]; nsmutableurlrequest *urlrequest = [nsmutableurlrequest requestwithurl:url]; [urlrequest settimeoutinterval:30.0f]; [urlrequest sethttpmethod:@"post"]; [urlrequest addvalue:@"test" forhttpheaderfield:@"m_username" ]; [urlrequest addvalue:@"123" forhttpheaderfield:@"m_password" ]; [[nsurlconnection alloc] initwithrequest:urlrequest delegate:self]; nsoperationqueue *queue = [[nsoperationqueue alloc] init]; [nsurlconnection sendasynchronousrequest:urlrequest queue:queue completionhandler:^(nsurlresponse *response,nsdata *data, nserror *error) { if ([data length] >0 && error == nil){ html = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; nslog(@"html = %@", html); receiveddata = [nsmutabledata data]; } else if ([data length] == 0 && error == nil){ nslog(@"nothing downloaded."); } else if (error != nil){ nslog(@"error happened = %@", error); } }]; // start loading data nsurlconnection *theconnection = [[nsurlconnection alloc] initwithrequest:urlrequest delegate:self]; if (theconnection) { // create nsmutabledata hold received data receiveddata = [nsmutabledata data]; } else { // inform user connection failed. }
[urlrequest addvalue:@"test" forhttpheaderfield:@"m_username" ]; [urlrequest addvalue:@"123" forhttpheaderfield:@"m_password" ];
this problem. you're sending values http header fields, not post data. post data goes in request body, not header. php doesn't treat incoming post fields @ in $_post
array. (those fields might turn in $_server
array, though... i've never tried that. it's not preferred method sending post data, anyhow.)
do instead:
nsstring *myrequeststring = @"m_username=test&m_password=123"; nsdata *myrequestdata = [nsdata datawithbytes: [myrequeststring utf8string] length: [myrequeststring length]]; [urlrequest sethttpbody: myrequestdata];
you can, obviously, compose myrequeststring
formatted string, dropping in values user provides, if needed.
also note since you're hitting non-ssl url, data sent in clear, known kids these days bad idea.
Comments
Post a Comment