Jump to content

PHP Unique view counter works correctly on localhost, not on server though.


shortysbest

Recommended Posts

My php unique view counter works and all, but on my localhost it only writes my ip once and counts the view once and then doesnt anymore  no matter how many times i refresh the page, but when i put it on my hosting server it counts my ip and other ip's over and over everytime it's viewed and it adds to the view counter. I'm hoping someone could help me out here.

 

the entire code is:

 

<?php
//open ip addres ip, count line
$ipfile = file("ips.txt");
$linecount = count($ipfile);
//open count file
$file = file_get_contents("count.txt");
$visitors = $file;
// set incremement boolean (should we add 1?)
$increment = true;
//users ip
$ip = $_SERVER['REMOTE_ADDR'];

if ($linecount==0)
$increment == true;
//start loop
for($x=0;$x<$linecount;$x++)
{
//does ip match that in file?
if(intval($ipfile[$x])==$ip)
$increment = false;

}
//open new file, write new visitors value
if ($increment == true)
{

$filenew = fopen("count.txt",'w');

$visitorsnew = $visitors + 1;
fwrite($filenew,$visitorsnew);

$filenewip = fopen("ips.txt",'a');
fwrite($filenewip,$ip."\n");

}

?>

Link to comment
Share on other sites

This way should work. Not sure what the intval() was for. Hope this works. I havent tested it.

 

<?php

error_reporting(E_ALL);

$ipfile = file_get_contents("ips.txt"); // Put ips.txt into string
$lines = explode("\r\n", $ipfile); // Split ip file into array at each new line (might be \r\n)

$file = file_get_contents("count.txt"); // get count file
$visitors = (int)$file; // Set to int

// set incremement boolean (should we add 1?)
$increment = true;

//users ip
$ip = $_SERVER['REMOTE_ADDR'];

if (!$lines)
{
    $increment = true;
}

//start loop
foreach($lines as $line)
{
    if($line == $ip)
    {
        $increment = false;
    }
}

//open new file, write new visitors value
if ($increment == true)
{
    $visitorsnew = $visitors + 1;
    file_put_contents("count.txt", $visitorsnew);
    file_put_contents("ips.txt", $ip . "\n", FILE_APPEND);
}

?>

Link to comment
Share on other sites

This way should work. Not sure what the intval() was for. Hope this works. I havent tested it.

 

<?php

error_reporting(E_ALL);

$ipfile = file_get_contents("ips.txt"); // Put ips.txt into string
$lines = explode("\r\n", $ipfile); // Split ip file into array at each new line (might be \r\n)

$file = file_get_contents("count.txt"); // get count file
$visitors = (int)$file; // Set to int

// set incremement boolean (should we add 1?)
$increment = true;

//users ip
$ip = $_SERVER['REMOTE_ADDR'];

if (!$lines)
{
    $increment = true;
}

//start loop
foreach($lines as $line)
{
    if($line == $ip)
    {
        $increment = false;
    }
}

//open new file, write new visitors value
if ($increment == true)
{
    $visitorsnew = $visitors + 1;
    file_put_contents("count.txt", $visitorsnew);
    file_put_contents("ips.txt", $ip . "\n", FILE_APPEND);
}

?>

 

This code still counts every view by me. it does exactly what my code did. :\

Link to comment
Share on other sites

using a data base would probably be alot easier. instead of looping through a file, simply do a query or 2 and your done.

 

off topic:

btw your quote

"War does not determine who is right... Only who is dead" - Unknown

 

i think it goes

"War does not determine who is right... Only who is left" - Unknown

 

Link to comment
Share on other sites

I actually just came up with another code that works correctly. Just it writes the ip over and over each time visited, but it doesnt change the view count variable.

 

<?php
$filename = "count.txt";

$file = file($filename);
$file = array_unique($file);
$hits = count($file);

$fd = fopen ($filename , "r");
$fstring = fread ($fd , filesize ($filename));
fclose($fd);
$fd = fopen ($filename , "w");
$fcounted = $fstring."\n".getenv("REMOTE_ADDR");
$fout= fwrite ($fd , $fcounted );
fclose($fd);
?>

Link to comment
Share on other sites

off topic:

btw your quote

"War does not determine who is right... Only who is dead" - Unknown

 

i think it goes

"War does not determine who is right... Only who is left" - Unknown

I no xD. But "Only who is dead" sounds better XD

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.