Jump to content

[SOLVED] Where's the error?


LemonInflux

Recommended Posts

Whys it got newlines at the start of the script? You haven't modified the script have you, except the additions I told you to make?

 

Change this part of the code to:

        // 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);
        }

to:

$i = 1; echo '<pre>';
        // Change the array so that it's keyed on IP and the value is the hits
        foreach ($data as $d)
        {
            $d = trim($d);
            if(!empty($d))
            {
                echo $i . ': ' . $d . "\n"; $i++;

                list($ip, $hits) = explode('|', $d);
                $new_data[$ip] = trim($hits);
            }else{$i++;}
        }

        echo '</pre>';

Re run the code.

Link to comment
Share on other sites

Never mind, never closed the <pre>.

 

OK, the hits counter worked, but the current content of the counter.php file is:

 

<?php die('You are not allowed to view this page'); ?>|

**.**.***.***|'number of hits'

 

A) why does the php die have a | straight after?

B) why can't multiple IPs be recorded?

Link to comment
Share on other sites

I dunno! My code doesn't add the line:

<?php die('You are not allowed to view this page'); ?>|

 

As for recording multiple ips it should do. Try hard coding a value for $_SERVER['REMOTE_ADDR'] variable, eg: add $_SERVER['REMOTE_ADDR'] = 'dummyip;

at the top of the script. Run the script then remove that line re run script and you should see your ip address logged as well as your hard coded one.

Link to comment
Share on other sites

My code at the moment:

 

<?php
if(file_exists('counter.php'))
{
    if(is_readable('counter.php') && is_writable('counter.php'))
    {

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

        $i = 1; echo '<pre>';
        // Change the array so that it's keyed on IP and the value is the hits
        foreach ($data as $d)
        {
            $d = trim($d);
            if(!empty($d))
            {
                list($ip, $hits) = explode('|', $d);
                $new_data[$ip] = trim($hits);
            }else{$i++;}
        }

        // 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']]++;

        }

        // 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);
    }
}
echo '</pre>';
?>

Link to comment
Share on other sites

You've removed the else statment from the if on this block of code:

// 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']]++;

        }

It should be:

// 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;
        }

Without the else newer IP's wont be added to current list. Only existing ips will be updated!

 

<?php

if(file_exists('counter.php'))
{
    if(is_readable('counter.php') && is_writable('counter.php'))
    {
        // Open the file and read the details into an array
        $data = file('counter.php');

        // remove first line from file, this is the PHP line to stop people from viewing contact.php
        array_shift($data);

        // Change the array so that it's keyed on IP and the value is the hits
        foreach ($data as $d)
        {
            $d = trim($d);
            if(!empty($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');

        // shtop people from viewing the file.
        fwrite($handle, "<?php die('You are not allowed to view this page'); ?>\n");

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

            fwrite($handle, $data);
        }

        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

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.