IOS full stack siege Lion (PHP) -- Session3-php tutorial

Source: Internet
Author: User
Tags php print server array
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.


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?


Obtaining 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.


Do 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 information

The 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.


Retrieving 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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.