How to read and add to an array then write it to a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • npm
    New Member
    • Apr 2007
    • 57

    How to read and add to an array then write it to a text file

    I'm setting up a polling system where voters can only vote once, based on their IP address.

    When the poll loads I want to open the text file with the list of blocked IPs to see if a voter has already voted. If not, they vote and then their IP should get added to the array and then the updated array get written back to the text file.

    Here's the code I have so far:
    Code:
    <?php
    // Get information from file //
    $IPs = "IPs.txt";
    $fp = fopen($IPs, "r");
    $ipList = fread($fp, filesize($IPs));
    fclose($fp);
    
    // Explode line breaks //
    $bannedIPs = explode("\n", $ipList);
    
    // Check for IP //
    if (in_array($ipNew, $ipList)) {
    	echo "Thanks! We've already counted your vote.";
    }
    else {
    	include("voteCode.php");
    }
    
    // Get new IP //
    $ipNew = $REMOTE_ADDR;
    echo "Your IP: " . $ipNew . "<br />";
    
    // Add new IP to array //
    $bannedIPs[] = $ipNew;
    
    // Write new array & close file //
    $fp = fopen("IPs.txt","w+");
    foreach($bannedIPs as $key => $value){
    	fwrite($fp,$value."\n");
    }
    fclose($fp);
    ?>
    I think I have it worked out until the point where I need to write the updated array, and then it's not working.

    Thanks!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    hm, wouldn’t it be easier to put the IPs into a database (esp. if you have thousands of IPs)? it would make adding/checking much easier.

    PS. won’t work, if the user has a dynamic IP.

    Comment

    • npm
      New Member
      • Apr 2007
      • 57

      #3
      The thing is that I'll be initially setting up over 100 separate polls (with more to come) and I'll need a text file for each poll to store the IPs of the voters.

      I know about the dynamic IP thing. It's not too big of a deal. This will just be to keep most people from voting a bunch of times on the same poll.

      Comment

      • npm
        New Member
        • Apr 2007
        • 57

        #4
        Well, now I've made some progress, but it's not quite there.

        Code:
        <?php
        // Get information from file //
        $IPs = "IPs.txt";
        $fp = fopen($IPs, "r");
        $ipList = fread($fp, filesize($IPs));
        fclose($fp);
        
        // Explode line breaks //
        $bannedIPs = explode("\n", $ipList);
        
        // Get new IP //
        $ipNew = $REMOTE_ADDR;
        
        // Check to see if IP is in list
        if (in_array($ipNew, $bannedIPs)) {
        	echo "Thanks! We've already counted your vote.";
        }
        else {
        	include("voteCode.php");
        }
        
        // Add new IP to array //
        array_push($bannedIPs, $ipNew);
        
        // Write new array & close file //
        $fp = fopen("IPs.txt", "w+");
        foreach($bannedIPs as $key => $value) {
        	fwrite($fp,$value . "\n");
        }
        fclose($fp);
        ?>
        I've gotten it to add the new IP to the list, and write the file, but if I started with this original text file:
        Code:
        11.111.11.111
        22.222.22.222
        33.333.33.333
        44.444.44.444
        55.555.55.555
        It will save the text file like this:
        Code:
        11.111.11.111
        22.222.22.222
        33.333.33.333
        44.444.44.444
        55.555.55.555
        66.666.66.666
        
        77.777.77.777
        
        88.888.88.888
        
        99.999.99.999
        
        etc.
        Instead of this:
        Code:
        11.111.11.111
        22.222.22.222
        33.333.33.333
        44.444.44.444
        55.555.55.555
        66.666.66.666
        77.777.77.777
        88.888.88.888
        99.999.99.999
        etc.
        So, it's sorta doing what I want, but not quite exactly.

        Comment

        • captainB
          New Member
          • Aug 2009
          • 23

          #5
          database

          so, why won't you use a database again? It seems to make sense to use one. you can have a polls table, a banned IPs table, and a 3rd table that ties them together. You would even get voting statistics this way, if you wanted.

          As for the text file, just by looking at your post, it seems like your new IPs contain a newline character which gets added.
          What would happen if you were to add the new IP directly to the end of the text file, instead of adding it to the array and re-writing the entire file every time a vote is made?

          I still think a database is much better here.
          I'd be happy to help you create one =)

          Comment

          Working...