Monday, 7 September 2015

How to Detect Request type in PHP

How to Detect Request type (GET OR Post ) in PHP

Question:How to Detect Request type (Get OR Post) in PHP?
$method = $_SERVER['REQUEST_METHOD'];
try {
    switch ($method) {
        case 'GET':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        case 'POST':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        case 'PUT':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        case 'HEAD':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        case 'DELETE':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        case 'OPTIONS':
            /** Add your code here **/
            
            /** Add your code here **/
            break;
        default:
            echo "$method method not defined";
            break;
    }
} catch (Exception $e) {
    echo $e->getMessage();
}



Question: How to get all post data in PHP?
$postData = array();
if(!empty($_POST)){
    $postData = $_POST;
}
print_r($postData );



Question: How to get all GET data in PHP?
$getData = array();
if(!empty($_GET)){
    $getData = $_GET;
}
print_r($getData );



Question: How to get Document root path in PHP?
echo $_SERVER['DOCUMENT_ROOT'];



Question: How to get Client Ip-Address in PHP?
echo $_SERVER['REMOTE_ADDR'];