سؤال

i am using below code for checking user login system by web services. all is ok when i used correct information in url but soon i used wrong it does not return me the error.

code:

function service_response($api_response){

   $http_response_code = array(
        200 => 'OK',
        400 => 'Bad Request',
        401 => 'Unauthorized',
        403 => 'Forbidden',
        404 => 'Not Found'
    );

    header('HTTP/1.1 '.$api_response['status'].' '.$http_response_code[ $api_response['status'] ]);
    header('Content-Type: application/json; charset=utf-8');
    $json_response = json_encode($api_response);
    echo $json_response;
    exit;
}

$api_response_code = array(
    0 => array('HTTP Response' => 400, 'Message' => 'Unknown Error'),
    1 => array('HTTP Response' => 200, 'Message' => 'Success'),
    2 => array('HTTP Response' => 403, 'Message' => 'HTTPS Required'),
    3 => array('HTTP Response' => 401, 'Message' => 'Authentication Required'),
    4 => array('HTTP Response' => 401, 'Message' => 'Authentication Failed'),
    5 => array('HTTP Response' => 404, 'Message' => 'Invalid Request'),
    6 => array('HTTP Response' => 400, 'Message' => 'Invalid Response Format')
);


if( isset($_GET['actionid']) && $_GET['actionid'] == 'login_user'){
    $email    = $_GET['email'];
    $password = $_GET['password'];
    $query    = mysql_query("SELECT user_id FROM user WHERE email = '".$email."' AND password = '".md5($password)."'");
    $data     = array();
        if(mysql_num_rows($query)>0) { 
            while($row = mysql_fetch_assoc($query)) {
                $data[] = $row;
            }
    $response['code']               = 1;
    $response['status']             = $api_response_code[$response['code']]['HTTP Response'];
    $response['response_message']   = $api_response_code[$response['code']]['Message'];
    $response['message']            = 'You are logged In successfully';
    $response['data']               = $data;
    service_response($response);    

        }else{

    $response['code']               = 0;
    $response['status']             = $api_response_code[$response['code']]['HTTP Response'];
    $response['response_message']   = $api_response_code[$response['code']]['Message'];
    $response['message']            = 'Please enter correct email address and password';
    $response['data']               = $data;
    service_response($response);
    }
}

return correct information:

{"code":1,"status":200,"response_message":"Success","message":"You are logged In successfully","data":[{"user_id":"72"}]}

But when i put wrong details in my url it returns:

Bad Request

while i want this: {"code":0,"status":404,"data":null}

url with correct info: http://xxxxxx/actions.php?actionid=login_user&email=mohsin@balianti.com&password=mohsin

url with wrong info: http://xxxxxx/actions.php?actionid=login_user&email=mohsin@balianti.com&password=ddfsdfdsf

can somebody please help me..

thank you all

هل كانت مفيدة؟

المحلول

In the else { } block you defined:

$response['code']               = 0;

This you are mapping to

0 => array('HTTP Response' => 400, 'Message' => 'Unknown Error'),

and 400 is mapped to

400 => 'Bad Request',

Proving your script works, wow!

If you want to change this, change your mappings:

$response['code']               = 3; // or 4 or whatever you define
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top