Jump to content

[SOLVED] Where's the error?


LemonInflux

Recommended Posts

I have built a hit counter that logs the IPs of visitors to the site, but only once. Here's the code:

 

<?PHP
$ip = $_SERVER['REMOTE_ADDR'];
$users = file("users.db.php");
$newuser = true;
foreach($users as $userind)
{
$userinfo = explode("|", $userind);
if($userinfo[0] == $ip)
{
	$newuser = false;
	break;
}
}
if($newuser)
{
$File = "counter.php";
$Handle = fopen($File, 'a');
$Data = $ip;
fwrite($Handle, $Data);
$Data = "|";
fwrite($Handle, $Data);
$Data = "0\n";
fwrite($Handle, $Data);
fclose($Handle);
}
?>

 

However, this doesn't record only once. This records every time they visit. Where's the error in this code?

Link to comment
Share on other sites

A) Phrased the SQL thing wrong. I meant I don't want to use SQL. Also, not sure why the users.db.php bit is in there o_O

 

<?PHP
$ip = $_SERVER['REMOTE_ADDR'];
$users = file_get_contents("counter.php");
$userline = explode("\n", $users);
$newuser = true;
foreach($userline as $userind)
{
$userinfo = explode("|", $userind);
if($userinfo[0] == $ip)
{
	$newuser = false;
	break;
}
}
if($newuser)
{
$Handle = fopen($users, 'a');
$Data = $ip;
fwrite($Handle, $Data);
$Data = "|";
fwrite($Handle, $Data);
$Data = "0\n";
fwrite($Handle, $Data);
fclose($Handle);
}
?>

 

New code. Although, still doesn't work. Now it's just not writing to the file.

Link to comment
Share on other sites

I'd go with something like this (untested)...

 

<?php

// Open the file and read the details into an array
$data = file('counter.php');

// Change the array so that it's keyed on IP and the value is the hits
foreach ($data as $d)
{
   list($ip, $hits) = explode('|', $d);
   $new_data[$ip] = $hits;
}

// Check if the ip already exists
if (in_array($_SERVER['REMOTE_ADDR'], array_keys($new_data)))
{
   // If it does then increase the hits by one
   $new_data[$_SERVER['REMOTE_ADDR']]++;
}
else
{
   // If it doesn't then add it
   $new_data[$_SERVER['REMOTE_ADDR']] = 1;
}

// Then here open your counter.php file and write it all back
// REMEMBER the data you want is now in new_data, not the original $data

?>

 

Regards

Huggie

Link to comment
Share on other sites

Huggies code had a slight bug in it. Try the following code:

<?php

// Open the file and read the details into an array
$data = file('counter.php');

// Change the array so that it's keyed on IP and the value is the hits
foreach ($data as $d)
{
   list($ip, $hits) = explode('|', $d);
   $new_data[$ip] = trim($hits);
}

// Check if the ip already exists
if (isset($new_data) && array_key_exists($_SERVER['REMOTE_ADDR'], $new_data))
{
   // If it does then increase the hits by one
   $new_data[$_SERVER['REMOTE_ADDR']]++;
}
else
{
   // If it doesn't then add it
   $new_data[$_SERVER['REMOTE_ADDR']] = 1;
}

// rewrite new data to counter.php
$handle = fopen('counter.php', 'w');

foreach($new_data as $ip => $hits)
{
    $data = $ip . '|' . $hits . "\n";
    fwrite($handle, $data);
}

fclose($handle);

?>[code]

[/code]

Link to comment
Share on other sites

Code works flawlessly here. I'm assuming contact.php is formatted like the following:

127.0.0.1|1
192.168.2.2|1

 

The code should automatically increase the hits for the ip address already logged, eg if a user comes to the script and they have an ip address of 127.0.0.1 for example the script will increment the current hits for that ip address by 1. Which will result in contact.php changing to:

127.0.0.1|2
192.168.2.2|1

 

If a user has an ip address not currently listed in counter.php then it'll get added to the bottom, eg:

127.0.0.1|2
192.168.2.2|1
xxx.xx.x.xxx|1

xxx.xx.x.xxx being the ip address.

Link to comment
Share on other sites

Interesting, looks like something is not quite write. Added in some echo's at different stages of the script to see what is happening.

 

Re run the following code:

<?php
echo 'PHP Version: ' . phpversion() . '<br />';

if(file_exists('counter.php'))
{
    echo 'counter.php exists';

    if(is_readable('counter.php') && is_writable('counter.php'))
    {
        echo ' and is readable and writable<br />';

        echo 'Opening counter.php<br />';

        // Open the file and read the details into an array
        $data = file('counter.php');

        // Change the array so that it's keyed on IP and the value is the hits
        foreach ($data as $d)
        {
           list($ip, $hits) = explode('|', $d);
           $new_data[$ip] = trim($hits);
        }

        echo 'Retrived ips: <pre>' . print_r($new_data, true) . '</pre><br />';

        // Check if the ip already exists
        if (isset($new_data) && array_key_exists($_SERVER['REMOTE_ADDR'], $new_data))
        {
           // If it does then increase the hits by one
           $new_data[$_SERVER['REMOTE_ADDR']]++;

           echo $_SERVER['REMOTE_ADDR'] . ' exists hits incremented (' . $new_data[$_SERVER['REMOTE_ADDR']] . ')<br />';
        }
        else
        {
           // If it doesn't then add it
           $new_data[$_SERVER['REMOTE_ADDR']] = 1;

           echo $_SERVER['REMOTE_ADDR'] . ' does not exists added to list:<pre>' . print_r($new_data, true) . '</pre>';
        }

        echo 'Rewriting data to counter.php<br />';

        // rewrite new data to counter.php
        $handle = fopen('counter.php', 'w');

        foreach($new_data as $ip => $hits)
        {
            $data = $ip . '|' . $hits . "\n";

            echo 'Adding: ' . $data . '<br />';

            fwrite($handle, $data);
        }

        echo 'Counter.php closed';

        fclose($handle);
    }
    else
    {
        echo 'counter.php is either not writable or readable!';
    }
}
else
{
    echo 'counter.php does not exists';
}

?>

Link to comment
Share on other sites

PHP Version: 4.4.4

counter.php exists and is readable and writable

Opening counter.php

Retrived ips:

 

Array

(

    [

] =>

    [84.71.128.104] => 0

)

 

 

84.71.128.104 exists hits incremented (1)

Rewriting data to counter.php

Adding: |

Adding: 84.71.128.104|1

Counter.php closed

 

 

very odd...

Link to comment
Share on other sites

Just did. I got this output:

 

PHP Version: 4.4.4

counter.php exists and is readable and writable

Opening counter.php

Retrived ips:

 

Array

(

    [

] =>

    [] =>

    [**.**.***.***] => 8

)

 

 

**.**.***.*** exists hits incremented (9)

Rewriting data to counter.php

Adding: |

Adding: |

Adding: **.**.***.***|1

Counter.php closed

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.