我是php和CI的新手我正在提供服务来检查android端的20/(11+25)=55.56
。当令牌过期时,那时我得到了istokenexpired
。我知道它为什么会来。当我从json malform exeption
检查相同的方法时,它给了我正确的JSON,但当我从postman
检查它时,它向我显示了如此多的HTML代码。我想因为它我在android中得到了json malform。任何人都可以检查我的CI代码并告诉我这是什么问题。
CI代码
hutl.it
Api_model
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."}
我确信postman没有显示正确的输出,因为这个HTML代码我在android中遇到JSON格式错误。
欢迎任何帮助。
答案 0 :(得分: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;
}
}
}
答案 1 :(得分: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;