2017-11-21 1 views
0

저는 PHP와 CI가 새롭습니다. 안드로이드 쪽에서 istokenexpired을 확인하는 서비스를 만들고 있습니다. 토큰이 만료되면 그 시간에 나는 json malform exeption을 얻습니다. 나는 그것이 왜 오는 지 압니다. 내가 postman에서 똑같은 방법을 검사하면 JSON이 올바르게 표시되지만, hutl.it에서 확인하면 너무 많은 HTML 코드가 표시됩니다. 나는 그것 때문에 안드로이드에 json malform을 얻고 있다고 생각합니다. 누구든지 내 CI 코드를 확인하고 문제가 무엇인지 말해 주실 수 있습니까?CI 방법이 잘못된 출력을 제공하고 Json 오류가 발생합니다.

CI 코드

public function istokenexpired(){ 
    $data = json_decode(file_get_contents('php://input'), TRUE); 
    $token = $data['token']; 
    $status = $this->Api_model->is_token_expired($token); 

    if($status) { 
     $result['status'] = 'success'; 
     $result['message'] = 'Token is alive.'; 
    } else { 
     $result['status'] = 'error'; 
     $result['message'] = 'Token is expired.'; 
    } 

    echo json_encode($result); 

} // istokenexpired 

//postman output 

{ 
    "status": "error", 
    "message": "Token is expired." 
} 

//hurl.it Output 

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;"> 
    <h4>A PHP Error was encountered</h4> 
    <p>Severity: Notice</p> 
    <p>Message: Undefined property: stdClass::$user_id</p> 
    <p>Filename: models/Api_model.php</p> 
    <p>Line Number: 42</p> 
    <p>Backtrace:</p> 
    <p style="margin-left:10px"> 
     File: C:\inetpub\vhosts\nullplex.com\records.swasth.net\application\models\Api_model.php<br /> 
     Line: 42<br /> 
     Function: _error_handler 
    </p> 

    <p style="margin-left:10px"> 
     File: C:\inetpub\vhosts\nullplex.com\records.swasth.net\application\controllers\Api.php<br /> 
     Line: 3242<br /> 
     Function: is_token_expired   
    </p> 

    <p style="margin-left:10px"> 
     File: C:\inetpub\vhosts\nullplex.com\records.swasth.net\index.php<br /> 
     Line: 319<br /> 
     Function: require_once   
    </p> 

</div>{"status":"error","message":"Token is expired."} 

Api_model

public function is_token_expired($token) { 
    $this->db->select('expiry_time'); 
    $this->db->from('tbl_tokens'); 
    $this->db->where('token', $token); 
    $result = $this->db->get()->row(); 

    if ($result != null) { 
     //print_r($result->expiry_time); 
     if ($result->expiry_time < date("Y-m-d h:i:s")) { 
      $this->db->where('user_id', $result->user_id); 
      $this->db->delete('tbl_tokens'); 
      return false; 

확실 우체부가 올바른 출력을 표시되지 않을 때문에이 HTML 코드로 내가 안드로이드에서 JSON 잘못된 예외를 얻고입니다.

어떤 도움을 환영합니다. 에서

+0

는 라인 (42)에 Api_model''에서 함수 ...의 – sintakonte

+2

가능한 복제 보여 시도 [PHP - 경고 - 정의되지 않은 속성을 : stdClass이 - 해결?] (https://stackoverflow.com/questions/2471605/php-warning-undefined-property-stdclass-fix) – sintakonte

+0

'Api_model' 모달을 게시하십시오. –

답변

0

당신의 Api_Model 다음

public function is_token_expired($token) 
{ 

    $query = $this->db 
     ->select('expiry_time') 
     ->from('tbl_tokens') 
     ->where('token',$token) 
     ->get(); 

    if ($query->num_rows() == 1) 
    { 
     $result = $query->row(); 
     if($result->expiry_time < date("Y-m-d h:i:s")) 
     { 
      $this->db->where('user_id', $result->user_id); 
      $this->db->delete('tbl_tokens'); 
      return false; 
     } 
    } 
} 
0
// first try to get valid token 
$liveToken = $this->db->get_where('tbl_tokens', ['token' => $token, 'expiry_time >= ' => date("Y-m-d h:i:s")]); 

$liveToken = $liveToken->row() ?? false; 

// if no valid token, delete any invalid from table, you don't need those there 
if (false === $liveToken) { 
    $this->db->delete('tbl_tokens', ['expiry_time < ' => date("Y-m-d h:i:s")]); 
} 

// return existing row or false 
return $liveToken;