Hello, i need a little help...
This is not really for a page counter but for a simple rating system,
this php page saves the number of votes to a txt file and to another txt file the total score, each vote has a value from 1 to 5.
I want the PHP script to test the ip of user before it saves this values into the txt files so i think i need the IP`s to be saved to a txt file and alwais before voting the script tests to see if the ip has already been saved to that txt file so that each visitor can vote only once.

Here is my script now, what should I add ? :

<?php

//this is the same code we used in average.php to get the values of votes and score
$filename = 'votes.txt';
$fp = fopen($filename, "r");
$votes = fread($fp, filesize($filename));
fclose($fp);

$filename = 'score.txt';
$fp = fopen($filename, "r");
$score = fread($fp, filesize($filename));
fclose($fp);

//Up votes by one and add the user's choice to the score

$votes++;

$score = $score + $choice;

$filename = 'votes.txt';
$fp = fopen($filename, "w");
fwrite($fp, "$votes");
fclose($fp);

$filename = 'score.txt';
$fp = fopen($filename, "w");
fwrite($fp, "$score");
fclose($fp);

?>

So can anyone tell me what to add in code so script tests the ip of user ?

Thank you.

    first, its not efficient to scan thru txt files for ips of users.

    anyway you might also want to write $SERVER['REMOTE_ADDR']
    to one of the text files.. Write txt file with delimeter.. something like
    $filename = 'votes.txt';
    $fp = fopen($filename, "w");
    $ipad = $
    SERVER['REMOTE_ADDR'];
    $temp = "$votes|*|$ipad";
    fwrite($fp, "$temp");
    fclose($fp);

    and before this script u may want to check IP ads with previously stored 1s...

    I might not recommend this way... coz static IPs are rare.. so if user changes his IP he can re-vote and also that this is inefficient... I would suggest that u set a cookie on client side..

    Hope this helps

    TommYNandA

      Thanks for reply, i am new to PHP, can`t you insert the code you said in my code ? because i understood that it needs some modifications.

      Thanks.

        okay I am gonna insert cookies in your code that does the work you want more efficient.

        <?php
        setcookie('checkvote',"1",time()+360000);
        //this is the same code we used in average.php to get the values of votes and score
        if(isset($_COOKIE['checkone']))
        {
        echo "You already voted for this";
        }
        else
        {
        $filename = 'votes.txt';
        $fp = fopen($filename, "r");
        $votes = fread($fp, filesize($filename));
        fclose($fp);

        $filename = 'score.txt';
        $fp = fopen($filename, "r");
        $score = fread($fp, filesize($filename));
        fclose($fp);

        //Up votes by one and add the user's choice to the score

        $votes++;

        $score = $score + $choice;

        $filename = 'votes.txt';
        $fp = fopen($filename, "w");
        fwrite($fp, "$votes");
        fclose($fp);

        $filename = 'score.txt';
        $fp = fopen($filename, "w");
        fwrite($fp, "$score");
        fclose($fp);
        }
        ?>

        That should do the work.. make sure u dont have any space before <?php .. or it will cause you annoying error "header already sent"

        Good Luck
        TommYNandA

          Write a Reply...