【发布时间】:2017-12-17 18:49:59
【问题描述】:
我遇到一个未知问题,我创建了一个连接到 Mysql 的 PHP API(Slim 框架 + Slim PDO)。我使用 Nginx 作为 HTTP 服务器。 API 使用“device-id”标头来识别客户端(Android 应用程序)。令人担忧的是,最近 android 应用程序的更新使得在启动这个应用程序时,如果用户未知,它现在会在结果 API 上发出 2 个异步请求我发现自己在表 users 中有两个条目携带相同的设备 ID
在中间件中
$user = new User($device_id, $ip);
在用户类中
function __construct($device_id, $ip)
{
$this->_device_id = $device_id;
$this->_ip = $ip;
if ($this->isExists())
$this->updateInfo();
else
$this->createUser();
}
private function isExists()
{
global $db_core;
$selectStatement = $db_core->select(array('id', 'current_group'))
->from('users')
->where('device_id', '=', $this->_device_id);
$stmt = $selectStatement->execute();
if ($stmt->rowCount() > 0)
{
$u = $stmt->fetch();
$this->_id = $u['id'];
$this->_current_group = $u['current_group'];
return true;
}
return false;
}
createUser() 函数在用户表中创建一个条目,其中包含设备 ID 以及日期等其他信息。
提前感谢您的帮助
【问题讨论】: