Простой PHP Уникальный счетчик посещений останавливается после двух подсчетов?

 <?php

$countfile = 'counter.txt';
$ipfile = 'ip.txt';function countint(){
$ip = $_SERVER['REMOTE_ADDR'];
global $countfile , $ipfile;if (!in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
$current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
file_put_contents($ipfile, $ip."\n", FILE_APPEND);
file_put_contents($countfile, ++$current);

}}

countint();
$value =file_get_contents($countfile);

?>

Это функция count.php, включенная вместе с двумя файлами ip.txt и counter.txt.

счетчик не идет мимо 2 хитов

после двух попаданий останавливается запись ip-адреса

0

Решение

Дайте это попробовать. Я добавил elseif а также in_array состояние elseif (in_array...

Sidenote: имеет смысл, что unique счетчик ударов останавливается после 1иначе это не было бы уникально.

Если вы хотите продолжать считать их, вы можете попробовать. Если это не сработает, как ожидалось, дайте мне знать, и я постараюсь изменить его или вообще удалить ответ.

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

$countfile = 'counter.txt';
$ipfile = 'ip.txt';

function countint(){
$ip = $_SERVER['REMOTE_ADDR'];
global $countfile , $ipfile;if (!in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
$current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
file_put_contents($ipfile, $ip."\n", FILE_APPEND);

file_put_contents($countfile, ++$current);

}elseif (in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
$current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
file_put_contents($ipfile, $ip."\n", FILE_APPEND);

file_put_contents($countfile, ++$current);

}

}

countint();
$value =file_get_contents($countfile);

?>
0

Другие решения

пришлось изменить! in_array на, если вы хотите неуникальный счетчик

<?php

$countfile = 'counter.txt';
$ipfile = 'ip.txt';function countint(){
$ip = $_SERVER['REMOTE_ADDR'];
global $countfile , $ipfile;if (in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
$current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
file_put_contents($ipfile, $ip."\n", FILE_APPEND);
file_put_contents($countfile, ++$current);

}}

countint();
$value =file_get_contents($countfile);

?>
0

<?php

$countfile = 'counter.txt';
$ipfile = 'ip.txt';function countint(){
$ip = $_SERVER['REMOTE_ADDR'];
global $countfile , $ipfile;if (!in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
$current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
file_put_contents($ipfile, $ip."\n", FILE_APPEND);
file_put_contents($countfile, ++$current);

}}

countint();
$value =file_get_contents($countfile);

?>

отлично работает для уникального счетчика IP-адресов ………………..
Если вам нужно увеличивать при каждом просмотре страницы, попробуйте ответ выше с оператором if else …………

0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector