I'm currently working on a hit counter that counts whenever a new visitor visits the page, So for each new visitor i create a new .txt file.
The file is stored as "hits/".$_SERVER['REMOTE_ADDR'].".txt".
Here's my code:
<?php
$visits = 0;
$hits = 0;
$fileResource = @fopen("hits/".$remote_addr.".txt","w+");
fwrite($fileResource,
"IP Adress:".$remote_addr."
Timestamp:".time().""
);
$fileResource = @fopen("hits/".$remote_addr.".txt","r");
$fileContent = @fread($fileResource,100000);
preg_match_all("/(.*):(.*)\n/i",$fileContent,$fileMatches);
foreach(glob("hits/*.txt") as $hit){
$hits++;
}
?>
So what i am trying to do is:
- Grab the contents from the file (Done)
- Format it so it can be displayed in an Admin Panel by getting what's between ":" and \n (Need help)
- Display it on the Admin Panel
EDIT: i was playing around with the preg_match_all(), And i found the sulution. It was as easy as removing the \n
try replacing \n with \\n in your match expression. \n is literal character n. \n is literal \ followed by n.