Post weird characters to a PHP script?
I have an iPhone app where my data is posted to a PHP file on a server via GET. However, after executing with weird characters, there is no server response. (I put a NSLog on what I get returned from the server).
Here's how I post my data to the server:
// St uploading to server
NSString *urlString = [NSString stringWithFormat:@"http://myflashpics.com/iphone_processes/upload.php?album=%@&id=%@&caption=%@&orient=%@&size=%d&device=iPhone",getAlbumID,theUserID,theCaption,getOrientation,imageSize];
NSLog(@"Calling URL: %@", urlString);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
And here's how I process 开发者_C百科server-side:
<?php
// Connect to MYSQL
$name = $_GET['name'];
mysql_query("INSERT INTO *** (name) VALUES ('$name')");
I really would like to fix this server-side if all possible. I submitted to apple and I am hoping ti fix without resubmitting it.
Please please please help! Thanks
Coulton
First off, if your action is creating a record, you should be doing a POST, not a GET.
Secondly, stop using the mysql_* functions and use PDO, with Parameterized queries.
If you're still having issues, use the php error_log() function in combination with var_export($var, true) to write out log messages to identify what PHP is retrieving.
That, combined with any errors/exceptions that are generated by PDO should give you an indication of where the problem is.
If you are using $_GET you should be url encoding your variables before sending them.
Anyways, you should be doing two things:
- escaping your request variables (GET or POST) with functions such as
mysql_escape_string
(http://php.net/manual/en/function.mysql-real-escape-string.php) before inserting it into the database - encoding your variables for weird characters to numeric unicode characters (I beleive that is the correct term) using
mb_convert_encoding($variable, 'HTML-ENTITIES', 'UTF-8');
before storing them in the database
Hope that helps.
精彩评论