views:

47

answers:

4

Hello there so I just setup this basic poll, I inspired myself from something I found out there, and it's just a basic ajax poll that waves the results in a text file.

Although I was wondering, since I do not want the user to simply mass-click to advantage / disadvantage the results, i thought about adding a new text file that could simply save the IP, one on each line, and then checks if it's already logged, if yes, display the results, if not, show the poll.

My lines of code to save the result are:

   <?php
$vote = $_REQUEST['vote'];

$filename = "votes.txt";
$content = file($filename);

$array = explode("-", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0)
  {
  $yes = $yes + 1;
  }
if ($vote == 1)
  {
  $no = $no + 1;
  }

$insert = $yes."-".$no;
$fp = fopen($filename,"w");
fputs($fp,$insert);
fclose($fp);
?>

So I'd like to know how I could check out the IPs, in the same way it does basically.

And I'm not interested in database, even for security measures, I'm alright with what Ive got.

Thanks to any help!

+2  A: 

easiest way is to write data to file is

file_put_contents($filename, $data)

and to read data from file

file_get_contents($filename);

To get IP Address of the user $_SERVER['REMOTE_ADDR']

See php manual for file_put_contents for more information and file_get_contents

Here is sample code

<?php

//  File path
$file = 'votedips.txt';

//  Get User's IP Address
$ip = $_SERVER['REMOTE_ADDR'];

//  Get data from file (if it exists) or initialize to empty string
$votedIps = file_exists($file) ? file_get_contents($file) : '';

//
$ips = explode("\n", $votedIps);
if (array_search($ip, $ips)) {
    //  USER VOTED
} else {
    $ips[] = $ip;
}

//  Write data to file
$data = implode("\n", $ips);
file_put_contents($file, $data);

?>
Alex
alright ill try this out, but i might also try the cookie method, what would be the easiest way to do it?
Alex Cane
there's an issue with the file_get_contents($file, null, $data ); i tried looking into it, maybe its the implode that is not used correctly, or even the $ips but i couldn't find, this is the reporting, file_get_contents() expects parameter 3 to be resource
Alex Cane
sorry buddy, I had a silly error in the code... I fixed and updated the code. It is fully functional right now. If you want to use cookie take a look at php documentation for `setcookie($name, $value, $expires)` http://us.php.net/manual/en/function.setcookie.php
Alex
thanks loads man, works 100%! i figured out ill let the user vote once a day, so at the end of the day ill clear the ip file.
Alex Cane
A: 

You can use file_get_contents to save the file's content into a variable and then use the strpos function to check if the IP exists in that variable.
For example:

$ipfile=file_get_contents('ip.txt');
if (strpos($ipfile, $_SERVER['REMOTE_ADDR'])!==FALSE) // show the results
else // show the poll
rhino
Thanks alot for this, I might check the cookie solution, because it is true it could start to take alot of time to load. I'll keep that method in mind, thanks again
Alex Cane
+2  A: 

To stop multiple votes, I'd set a cookie once a user has voted. If the user reloads the page with the voting form on it and has a cookie, you could show just the results, or a "You have already voted." message. Note that this will not stop craftier people from double-voting - all they would have to do is remove the saved cookie, and they could re-vote.

Keep in mind though that IPs can be shared so your idea of storing IPs might backfire - people on a shared external-facing IP won't be able to vote, as your system will have registered a previous vote from someone at the same IP address.

Slater
You're right about the IP, but the idea about cookies is not really good. I know it from my own experience - I've once created a poll and it used cookies to prevent the users from double-voting. That was a fail as many users just switched off their cookies support for my domain and they voted as many times as they wanted to.
rhino
alright, i might try that actually, might save load time, thanks!
Alex Cane
there also should be a possebility to check whether someone has cookie activated or not and if not just forbid to take the poll
ITroubs
@ITroubs: and what good would that do? It'd be trivial to turn on cookies, vote, delete the "I voted!" cookie, and go back over and over.
Marc B
shure you can do that BUT the time you need to push the poll is extended because you have to have cookies activated and manually delete the cookie after every vote and not like in rhino's case where you just deactivate your cookies and then hit the F5 button till your finger bleeds
ITroubs
A: 

Be careful with storing IPs in a text file, and then using file_get_contents() and similar functions for loading the data/parseing. As an absolute worst case, assuming that every possible IP address used your system to vote, you'd end up with a text file in the many many gigabytes in size, and you'd exceed PHP's memory_limit very quickly.

Marc B