IOS full-stack siege Lion (PHP) -- Session3 processes client input What?
- Client IP
- URI (resource path) accessed by the client)
- User-Agent
- GET parameters (parameters passed in using URLs)
- POST parameters (parameters input using HTTP Body) are common client input content and are various data sent to the server during client access to the server.
How? GET
It is very easy to GET input using PHP. GET and POST parameters can be obtained using $ _ GET or $ _ POST. they are an array.
Now we use the iOS client to send a request to http: // localhost/index. php? Name = Pony
// Swift Playground // you can create a Playground in Xcode and run the following code import UIKitlet request = NSMutableURLRequest (URL: NSURL (string: "http: // localhost/index. php? Name = Pony ")!) NSURLConnection. sendAsynchronousRequest (request, queue: NSOperationQueue. mainQueue () {(response, responseData, error)-> Void in if let responseData = responseData {if let responseString = String (data: responseData, encoding: NSUTF8StringEncoding) {print (responseString) }}nsunloop. currentRunLoop (). runUntilDate (NSDate (timeIntervalSinceNow: 10 ))
This is a very simple request. we only need to send a GET request to http: // localhost/index. php, and then carry the GET parameter, Key = name Value = Pony. Finally, we get the data returned by the server and convert it to String for printing. If no exceptions occur, you can view the returned results in Playground.
Now, modify index. php so that index. php can obtain the Pony string and return the processed string to the client.
// Index. php
Run Playground again. you can see Hello, Pony! It is displayed in the result bar.
Result
It's really easy to GET the GET parameter, and so on. now, it's better than the product manager to add a requirement. if the name is blank, please return "Nobody! ".
We can slightly modify the code and use empty () to judge $ _ GET ["name"]. here is how to use empty.
Try removing Pony from the URL to see the execution result?
POSTObtaining parameters in the POST form is exactly the same as obtaining the GET method! However, we must first modify the code of the request in Playground to send a POST request.
import UIKitlet request = NSMutableURLRequest(URL: NSURL(string: "http://localhost/index.php")!)request.HTTPMethod = "POST"request.HTTPBody = "name=Pony".dataUsingEncoding(NSUTF8StringEncoding)NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, responseData, error) -> Void in if let responseData = responseData { if let responseString = String(data: responseData, encoding: NSUTF8StringEncoding) { print(responseString) } }}NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 10))
Then, replace $ _ GET in index. php with $ _ POST.
Run Playground to see the effect.
PrintDo you remember that $ _ GET and $ _ POST are arrays? In the PHP world, you can use the print_r () function to print all information about the array. you can see all the parameters passed in by the current client, which is very convenient for debugging applications.
The client receives the following PHP print results:
Array( [name] => Pony)
Extension: Get client informationThe client information also includes User-Agent and IP addresses. The information can be obtained using the $ _ SERVER array, and its retrieval method is the same as get post, you can obtain the client IP address or User-Agent by yourself.
EndingRetrieving client input is an important step in server development. here, we do not filter user input. it is very dangerous to use data that has not been filtered, I will explain it in detail in subsequent courses. At the same time, this course does not describe how to obtain the HTTPBody method in the POST request, which will be explained in the subsequent courses.
Next Article: