Привет, я пытаюсь сделать функцию count count, основанную на структуре codeigniter, и то, что я сделал до сих пор, отлично смотрится на моей стороне. Функция завершена, и все работает нормально.
Но у меня есть вопрос от некоторых экспертов, чтобы сказать мне, что это правильное использование функции, а не вышеизложенное вредит моей загрузке страницы. Функция доступна функцией jQuery get при каждом нажатии на сообщение.
Сначала я проверяю, есть ли IP-адрес, и если введенная дата ip-адреса больше 24 часов, если его больше я удаляю текущую строку, а затем снова проверяю, потому что предыдущий выбирает ее все еще помнящий последний ip-адрес и вставляя снова с новым временем.
И другой вопрос должен я делать cleanjob каждую неделю или подобное для всех ip addreses?
Вот мой код:
function show_count($post_id){
$ip = $this->input->ip_address();
$this->db->select('ip_address,data');
$this->db->from('post_views');
$this->db->where('ip_address',$ip);
$query = $this->db->get();
foreach ($query->result() as $row){
$ip_address = $row->ip_address;
$data = $row->data;
}
if(isset($ip_address) && time() >= strtotime($data) + 8640){
$this->db->where('ip_address',$ip);
$this->db->delete('post_views');
}
$this->db->select('ip_address');
$this->db->from('post_views');
$this->db->where('ip_address',$ip);
$query = $this->db->get();
foreach ($query->result() as $row){
$ip_address_new = $row->ip_address;
}
if(!isset($ip_address_new) && $ip_address_new == false){
$date = new DateTime('now', new DateTimeZone('Europe/Skopje'));
$this->db->set('views', 'views+ 1', false);
$this->db->where('post_id',$post_id);
$this->db->update('posts');
$data = array(
'ip_address'=>$ip,
'data'=>$date->format("Y-m-d H:i:s")
);
$this->db->insert('post_views',$data);
}
}
Спасибо, любые предложения будут оценены.
Вместо того, чтобы делать много запросов для увеличения уникальных просмотров в ваших сообщениях, вы должны использовать и устанавливать файлы cookie и иметь резервный метод, если файлы cookie не включены.
$post_id = "???"
if(isset($_COOKIE['read_articles'])) {
//grab the JSON encoded data from the cookie and decode into PHP Array
$read_articles = json_decode($_COOKIE['read_articles'], true);
if(isset($read_articles[$post_id]) AND $read_articles[$post_id] == 1) {
//this post has already been read
} else {
//increment the post view count by 1 using update queries
$read_articles[$post_id] = 1;
//set the cookie again with the new article ID set to 1
setcookie("read_articles",json_encode($read_articles),time()+60*60*24);
}
} else {
//hasn't read an article in 24 hours?
//increment the post view count
$read_articles = Array();
$read_articles[$post_id] = 1;
setcookie("read_articles",json_encode($read_articles),time()+60*60*24);
}