John Davidson

When I remove/replace a line it leaves the line in file? Live visitor counter php

0 comments
Message:


So currently I'm working on a live visitor counter for my website mostly just to add things but I have found a few sources but they don't work, I have rewritten multiple sources and here's my current code.


<?php
function livecount() {
$dbfiledirectory = $_SERVER['DOCUMENT_ROOT'] . "/inc/livecountdb.txt";
$time_out = 140; // 140 seconds until we recognize them as gone.
$getdbinformation = file($dbfiledirectory);



foreach ($getdbinformation as $lines){
$splittedtime = explode(";",$lines);
if ($splittedtime[0] + $time_out< date('his', time())){
$changing = file_get_contents($dbfiledirectory);
$contents = str_replace($lines, '', $changing);
file_put_contents($dbfiledirectory, $contents);
}
}
$contentsdir = file_get_contents($dbfiledirectory);
$pattern = preg_quote(get_client_ip(), '/');
$pattern = "/^.*$pattern.*\$/m";
if (preg_match($pattern, $contentsdir, $matches)) {
$changing = file_get_contents($dbfiledirectory);
$contents = str_replace($matches[0], '', $changing);
file_put_contents($dbfiledirectory, $contents);
} else {
$ip = date('his', time()) . ";" . get_client_ip();

$myFile2 = $dbfiledirectory;
$myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file.");
fwrite($myFileLink2, $ip);
fclose($myFileLink2);
}

echo count (file($dbfiledirectory));
}

function get_client_ip()
{
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if (getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if (getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if (getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if (getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if (getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}

The problem I'm having is when it removes/replaces the IP;time it still leaves a line in the txt file which causes an error.


Ps - sorry for the sloppy code, I've been on this for hours and its had better days.



My Answer:
It looks like the issue might be with how you are removing the line from the file. Instead of using `str_replace` to remove the line, you can use `unset` to remove the line from the array and then write the updated array back to the file.

Here's an updated version of your code that should remove the line completely from the file:

php
function livecount() {
$dbfiledirectory = $_SERVER['DOCUMENT_ROOT'] . "/inc/livecountdb.txt";
$time_out = 140; // 140 seconds until we recognize them as gone.
$getdbinformation = file($dbfiledirectory);

foreach ($getdbinformation as $key => $line){
$splittedtime = explode(";", $line);
if ($splittedtime[0] + $time_out < date('his', time())){
unset($getdbinformation[$key]);
}
}

$contentsdir = implode("", $getdbinformation);
$pattern = preg_quote(get_client_ip(), '/');
$pattern = "/^.*$pattern.*$/m";
if (preg_match($pattern, $contentsdir, $matches)) {
$getdbinformation = array_values($getdbinformation);
$key = array_search($matches[0], $getdbinformation);
unset($getdbinformation[$key]);
} else {
$ip = date('his', time()) . ";" . get_client_ip();
$getdbinformation[] = $ip;
}

file_put_contents($dbfiledirectory, implode("\n", $getdbinformation));

echo count($getdbinformation);
}


This code should remove the line completely from the file when it is replaced or removed. Let me know if you have any questions or if you encounter any issues.

Rate this post

4 of 5 based on 2613 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us