Я сейчас пишу hook
для отслеживания активности пользователей и сохранения ее в базе данных. Проблема, с которой я сталкиваюсь, заключается в том, что всякий раз, когда я загружаю страницу, я получаю несколько вставок в поддокумент page_views
. Вставки могут содержать все от текущей страницы, которую я просматриваю, до любой из других страниц, которые я просматривал недавно. Я попытался вызвать ловушку на следующих этапах: post_controller_constructor
, post_controller
и post_system
; все с одной и той же проблемой.
Hook Class:
class User_tracking
{
var $CI;
public function __construct()
{
$this->CI =& get_instance();
}
public function track_page_view()
{
if(!$this->CI->session->has_userdata('tracking_id'))
{
$this->_generate_tracking_id();
}
$url = site_url($this->CI->uri->uri_string());
if(!preg_match('/(xhr|sitemap|rss|embed)/', $url))
{
$where = [ '_id' => $this->CI->session->userdata('tracking_id') ];
$update = [
'$push' => [
'page_views' => [
'url' => $url,
'updated_on' => time()
]
]
];
$this->CI->mongo->update_one('tracking', $where, $update);
}
}
private function _generate_tracking_id()
{
$insert = [ 'created_on' => time() ];
$tracking_id = $this->CI->mongo->insert('tracking', $insert);
$this->CI->session->set_userdata('tracking_id', $tracking_id);
}
}
Пример документа базы данных (с комментариями):
{
'_id': ObjectId('5ea760f5c73ae92fbf77f612'),
'created_on': 1588027637,
'page_views': [
// Calling 'https://www.url.test/'
0: {
'url': 'https://www.url.test/',
'updated_on': 1588027637
},
1: {
'url': 'https://www.url.test/',
'updated_on': 1588027639
},
// Calling 'https://www.url.test/las-vegas/'
2: {
'url': 'https://www.url.test/',
'updated_on': 1588034596
},
3: {
'url': 'https://www.url.test/las-vegas/',
'updated_on': 1588034599
},
4: {
'url': 'https://www.url.test/',
'updated_on': 1588034600
},
// Calling 'https://www.url.test/los-angeles/'
5: {
'url': 'https://www.url.test/las-vegas/',
'updated_on': 1588035741
},
6: {
'url': 'https://www.url.test/',
'updated_on': 1588035743
},
7: {
'url': 'https://www.url.test/los-angeles/',
'updated_on': 1588035744
},
8: {
'url': 'https://www.url.test/las-vegas/',
'updated_on': 1588035746
},
9: {
'url': 'https://www.url.test/los-angeles/',
'updated_on': 1588035749
},
]
}
Я потратил слишком много времени по этому вопросу и добились нулевого прогресса. Я даже пытался переместить этот скрипт в модель и вызывать модель с каждого контроллера с той же проблемой. Когда я делаю что-то вроде echo $url;
в track_page_view()
, я получаю только один вывод (правильный URL), и это единственная вещь на всем сайте, которая ведет себя так:
- EDIT -
В верхней части метода User_tracking::track_page_view()
я сделал следующее: error_log(json_encode($_SERVER));
. Я загрузил страницу только один раз, и вот что вернул журнал ошибок.
[Tue May 05 14:18:40.857118 2020] [php7:notice] [pid 264] [client 127.0.0.1:61487] {"HTTPS":"on","SSL_TLS_SNI":"www.bid.test","HTTP_HOST":"www.bid.test","HTTP_CONNECTION":"keep-alive","HTTP_USER_AGENT":"Mozilla\\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\\/537.36 (KHTML, like Gecko) Chrome\\/84.0.4136.1 Safari\\/537.36","HTTP_ACCEPT":"*\\/*","HTTP_SEC_FETCH_SITE":"none","HTTP_SEC_FETCH_MODE":"cors","HTTP_SEC_FETCH_DEST":"empty","HTTP_ACCEPT_ENCODING":"gzip, deflate, br","HTTP_ACCEPT_LANGUAGE":"en-US,en;q=0.9","HTTP_COOKIE":"bid_dev_session=ommlsr1ggs49v9b7a1blenait8fc8j35","PATH":"\\/usr\\/local\\/sbin:\\/usr\\/local\\/bin:\\/usr\\/sbin:\\/usr\\/bin:\\/sbin:\\/bin","SERVER_SIGNATURE":"<address>Apache\\/2.4.29 (Ubuntu) Server at www.bid.test Port 443<\\/address>\\n","SERVER_SOFTWARE":"Apache\\/2.4.29 (Ubuntu)","SERVER_NAME":"www.bid.test","SERVER_ADDR":"127.0.0.1","SERVER_PORT":"443","REMOTE_ADDR":"127.0.0.1","DOCUMENT_ROOT":"\\/mnt\\/c\\/dev\\/php\\/bidfront","REQUEST_SCHEME":"https","CONTEXT_PREFIX":"","CONTEXT_DOCUMENT_ROOT":"\\/mnt\\/c\\/dev\\/php\\/bidfront","SERVER_ADMIN":"webmaster@localhost","SCRIPT_FILENAME":"\\/mnt\\/c\\/dev\\/php\\/bidfront\\/index.php","REMOTE_PORT":"61487","GATEWAY_INTERFACE":"CGI\\/1.1","SERVER_PROTOCOL":"HTTP\\/1.1","REQUEST_METHOD":"GET","QUERY_STRING":"","REQUEST_URI":"\\/","SCRIPT_NAME":"\\/index.php","PHP_SELF":"\\/index.php","REQUEST_TIME_FLOAT":1588713520.112,"REQUEST_TIME":1588713520,"CI_ENV":"development"}
[Tue May 05 14:18:41.150063 2020] [php7:notice] [pid 260] [client 127.0.0.1:61390] {"HTTPS":"on","SSL_TLS_SNI":"www.bid.test","HTTP_HOST":"www.bid.test","HTTP_CONNECTION":"keep-alive","HTTP_USER_AGENT":"Mozilla\\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\\/537.36 (KHTML, like Gecko) Chrome\\/84.0.4136.1 Safari\\/537.36","HTTP_ACCEPT":"*\\/*","HTTP_SEC_FETCH_SITE":"none","HTTP_SEC_FETCH_MODE":"cors","HTTP_SEC_FETCH_DEST":"empty","HTTP_ACCEPT_ENCODING":"gzip, deflate, br","HTTP_ACCEPT_LANGUAGE":"en-US,en;q=0.9","HTTP_COOKIE":"bid_dev_session=ommlsr1ggs49v9b7a1blenait8fc8j35","PATH":"\\/usr\\/local\\/sbin:\\/usr\\/local\\/bin:\\/usr\\/sbin:\\/usr\\/bin:\\/sbin:\\/bin","SERVER_SIGNATURE":"<address>Apache\\/2.4.29 (Ubuntu) Server at www.bid.test Port 443<\\/address>\\n","SERVER_SOFTWARE":"Apache\\/2.4.29 (Ubuntu)","SERVER_NAME":"www.bid.test","SERVER_ADDR":"127.0.0.1","SERVER_PORT":"443","REMOTE_ADDR":"127.0.0.1","DOCUMENT_ROOT":"\\/mnt\\/c\\/dev\\/php\\/bidfront","REQUEST_SCHEME":"https","CONTEXT_PREFIX":"","CONTEXT_DOCUMENT_ROOT":"\\/mnt\\/c\\/dev\\/php\\/bidfront","SERVER_ADMIN":"webmaster@localhost","SCRIPT_FILENAME":"\\/mnt\\/c\\/dev\\/php\\/bidfront\\/index.php","REMOTE_PORT":"61390","GATEWAY_INTERFACE":"CGI\\/1.1","SERVER_PROTOCOL":"HTTP\\/1.1","REQUEST_METHOD":"GET","QUERY_STRING":"","REQUEST_URI":"\\/","SCRIPT_NAME":"\\/index.php","PHP_SELF":"\\/index.php","REQUEST_TIME_FLOAT":1588713520.858,"REQUEST_TIME":1588713520,"CI_ENV":"development"}
[Tue May 05 14:18:41.448242 2020] [php7:notice] [pid 264] [client 127.0.0.1:61487] {"HTTPS":"on","SSL_TLS_SNI":"www.bid.test","HTTP_HOST":"www.bid.test","HTTP_CONNECTION":"keep-alive","HTTP_USER_AGENT":"Mozilla\\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\\/537.36 (KHTML, like Gecko) Chrome\\/84.0.4136.1 Safari\\/537.36","HTTP_ACCEPT":"*\\/*","HTTP_SEC_FETCH_SITE":"none","HTTP_SEC_FETCH_MODE":"cors","HTTP_SEC_FETCH_DEST":"empty","HTTP_ACCEPT_ENCODING":"gzip, deflate, br","HTTP_ACCEPT_LANGUAGE":"en-US,en;q=0.9","HTTP_COOKIE":"bid_dev_session=ommlsr1ggs49v9b7a1blenait8fc8j35","PATH":"\\/usr\\/local\\/sbin:\\/usr\\/local\\/bin:\\/usr\\/sbin:\\/usr\\/bin:\\/sbin:\\/bin","SERVER_SIGNATURE":"<address>Apache\\/2.4.29 (Ubuntu) Server at www.bid.test Port 443<\\/address>\\n","SERVER_SOFTWARE":"Apache\\/2.4.29 (Ubuntu)","SERVER_NAME":"www.bid.test","SERVER_ADDR":"127.0.0.1","SERVER_PORT":"443","REMOTE_ADDR":"127.0.0.1","DOCUMENT_ROOT":"\\/mnt\\/c\\/dev\\/php\\/bidfront","REQUEST_SCHEME":"https","CONTEXT_PREFIX":"","CONTEXT_DOCUMENT_ROOT":"\\/mnt\\/c\\/dev\\/php\\/bidfront","SERVER_ADMIN":"webmaster@localhost","SCRIPT_FILENAME":"\\/mnt\\/c\\/dev\\/php\\/bidfront\\/index.php","REMOTE_PORT":"61487","GATEWAY_INTERFACE":"CGI\\/1.1","SERVER_PROTOCOL":"HTTP\\/1.1","REQUEST_METHOD":"GET","QUERY_STRING":"","REQUEST_URI":"\\/","SCRIPT_NAME":"\\/index.php","PHP_SELF":"\\/index.php","REQUEST_TIME_FLOAT":1588713521.151,"REQUEST_TIME":1588713521,"CI_ENV":"development"}
[Tue May 05 14:18:41.751220 2020] [php7:notice] [pid 260] [client 127.0.0.1:61390] {"HTTPS":"on","SSL_TLS_SNI":"www.bid.test","HTTP_HOST":"www.bid.test","HTTP_CONNECTION":"keep-alive","HTTP_USER_AGENT":"Mozilla\\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\\/537.36 (KHTML, like Gecko) Chrome\\/84.0.4136.1 Safari\\/537.36","HTTP_ACCEPT":"*\\/*","HTTP_SEC_FETCH_SITE":"none","HTTP_SEC_FETCH_MODE":"cors","HTTP_SEC_FETCH_DEST":"empty","HTTP_ACCEPT_ENCODING":"gzip, deflate, br","HTTP_ACCEPT_LANGUAGE":"en-US,en;q=0.9","HTTP_COOKIE":"bid_dev_session=ommlsr1ggs49v9b7a1blenait8fc8j35","PATH":"\\/usr\\/local\\/sbin:\\/usr\\/local\\/bin:\\/usr\\/sbin:\\/usr\\/bin:\\/sbin:\\/bin","SERVER_SIGNATURE":"<address>Apache\\/2.4.29 (Ubuntu) Server at www.bid.test Port 443<\\/address>\\n","SERVER_SOFTWARE":"Apache\\/2.4.29 (Ubuntu)","SERVER_NAME":"www.bid.test","SERVER_ADDR":"127.0.0.1","SERVER_PORT":"443","REMOTE_ADDR":"127.0.0.1","DOCUMENT_ROOT":"\\/mnt\\/c\\/dev\\/php\\/bidfront","REQUEST_SCHEME":"https","CONTEXT_PREFIX":"","CONTEXT_DOCUMENT_ROOT":"\\/mnt\\/c\\/dev\\/php\\/bidfront","SERVER_ADMIN":"webmaster@localhost","SCRIPT_FILENAME":"\\/mnt\\/c\\/dev\\/php\\/bidfront\\/index.php","REMOTE_PORT":"61390","GATEWAY_INTERFACE":"CGI\\/1.1","SERVER_PROTOCOL":"HTTP\\/1.1","REQUEST_METHOD":"GET","QUERY_STRING":"","REQUEST_URI":"\\/","SCRIPT_NAME":"\\/index.php","PHP_SELF":"\\/index.php","REQUEST_TIME_FLOAT":1588713521.449,"REQUEST_TIME":1588713521,"CI_ENV":"development"}
[Tue May 05 14:18:42.057710 2020] [php7:notice] [pid 264] [client 127.0.0.1:61487] {"HTTPS":"on","SSL_TLS_SNI":"www.bid.test","HTTP_HOST":"www.bid.test","HTTP_CONNECTION":"keep-alive","HTTP_USER_AGENT":"Mozilla\\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\\/537.36 (KHTML, like Gecko) Chrome\\/84.0.4136.1 Safari\\/537.36","HTTP_ACCEPT":"*\\/*","HTTP_SEC_FETCH_SITE":"none","HTTP_SEC_FETCH_MODE":"cors","HTTP_SEC_FETCH_DEST":"empty","HTTP_ACCEPT_ENCODING":"gzip, deflate, br","HTTP_ACCEPT_LANGUAGE":"en-US,en;q=0.9","HTTP_COOKIE":"bid_dev_session=ommlsr1ggs49v9b7a1blenait8fc8j35","PATH":"\\/usr\\/local\\/sbin:\\/usr\\/local\\/bin:\\/usr\\/sbin:\\/usr\\/bin:\\/sbin:\\/bin","SERVER_SIGNATURE":"<address>Apache\\/2.4.29 (Ubuntu) Server at www.bid.test Port 443<\\/address>\\n","SERVER_SOFTWARE":"Apache\\/2.4.29 (Ubuntu)","SERVER_NAME":"www.bid.test","SERVER_ADDR":"127.0.0.1","SERVER_PORT":"443","REMOTE_ADDR":"127.0.0.1","DOCUMENT_ROOT":"\\/mnt\\/c\\/dev\\/php\\/bidfront","REQUEST_SCHEME":"https","CONTEXT_PREFIX":"","CONTEXT_DOCUMENT_ROOT":"\\/mnt\\/c\\/dev\\/php\\/bidfront","SERVER_ADMIN":"webmaster@localhost","SCRIPT_FILENAME":"\\/mnt\\/c\\/dev\\/php\\/bidfront\\/index.php","REMOTE_PORT":"61487","GATEWAY_INTERFACE":"CGI\\/1.1","SERVER_PROTOCOL":"HTTP\\/1.1","REQUEST_METHOD":"GET","QUERY_STRING":"","REQUEST_URI":"\\/","SCRIPT_NAME":"\\/index.php","PHP_SELF":"\\/index.php","REQUEST_TIME_FLOAT":1588713521.752,"REQUEST_TIME":1588713521,"CI_ENV":"development"}