Solved How do I log IPs and redirect if IP is logged

May 26, 2011 at 14:12:36
Specs: Windows Vista, 2GB
I am in the process of developing a website that basically just has a bunch of polls, but I want them to be spammed. So what I have right now is when a user votes, it adds one to a text file that started from 0, which records how many votes. However, I do not want users to vote more than once, so it is currently just directing to a page with the results, but if they hit the back button, they can still vote again. So what I want is when it logs the "+1" and then redirects, I also want it to log the IP address to a different file and then when the page is loaded, redirect to the results page if the IP address is logged in the text file. Is that possible? If so please show me how.

I guess another question I could ask is how can I make my own poll generator? My website will be filled with them and I don't want to use an external poll generator and then link it in my website all the time. I kind of figured out how to make the poll, but like I stated in the previous sentence, it is just a link and then counts up from 0 each time it is clicked.

I am not sure if this makes sense to anyone beside me, so if you think you know how to fix my problem, but you need more info or understanding or something, just ask me for it. Thanks.

Oh, and it would be preferred if this could be done with PHP.


See More: How do I log IPs and redirect if IP is logged

Report •


#1
May 28, 2011 at 21:32:15
If you have access to a database, you can probably implement your needs a lot eaiser - both in logging the IPs and keeping a count of poll results.

If you have to use a flat file, then I'm not sure if it will be of any pratical use, but for the first part, I thought maybe you could log the ip along with which poll(s), if any, the visitor has already taken since you wanted multiple polls.

Maybe something like below will help or perhaps spark an idea though it does NO error checking of any kind.

<?php

   define("POLL1",       1);
   define("POLL2",       2);
   define("POLL3",       4);
   define("POLL4",       8);
   define("POLL5",      16);
   define("POLLN", "value");
   define("MAXSTR",     32);   
   
   srand();

   $fh = @fopen("iplog.txt", "w+"); // testing; contents change with page load
   $i = 0;
   $find = "";
   $poll_taken = "POLL" . rand(1,5);

   if($fh) {
      while($i < 100) { // write 100 ips (may not be unique!)
         $tmp = rand(1,255) . "." . rand(1,255) . "." . rand(1,255) . "." . rand(1,255) . ":" . rand(1, 16);
         pad(&$tmp);
         fwrite($fh, $tmp);
         ++$i;
         if($i == 36) $find = substr($tmp, 0, strpos($tmp, ':')); // pretend this is the current visitor
         unset($tmp);
      }

      rewind($fh);
      fseek($fh, 0, SEEK_END);
      $nrec = ftell($fh); // get file size or use filesize("path/filename"); total stored ips = ftell($fh) / MAXSTR
      $crec = 0;
      rewind($fh);

      for($j=1; $j <= 5; ++$j)
         echo "POLL$j = " . constant("POLL" . $j) . "<br />";

      echo "voting in $poll_taken<br>";

      while($crec < $nrec) {

         $line = fgets($fh, MAXSTR + 1);
         $data = split(":", $line);
         $completed_polls = trim($data[1], "*");
         settype($completed_polls, "integer");

         if(strcmp($data[0], $find) == 0) {

            if((constant($poll_taken) & $completed_polls) == constant($poll_taken)) {
               echo "ip = " . $data[0] . "   polls taken cumlative: " . trim($data[1], "*") . "<br>";
               echo "sorry; you've already voted in this poll";
               break;
            } else {
               echo "ip = " . $data[0] . "   polls taken cumlative: " . trim($data[1], "*") . "<br>";
               echo "you may vote in this poll";
               break;
            }
         }

         $crec += MAXSTR;
         fseek($fh, $crec, SEEK_SET);         
      }

      fclose($fh);      
   }
      

function pad($source) {

   for($i=strlen($source); $i < MAXSTR; ++$i)
      $source .= "*";
}     
    
?>           

For a poll generator, I'm not entirely sure what you mean. Do you mean create the polls or just pick and display a random poll that exists already?

HTH


Report •

#2
May 29, 2011 at 00:05:09
Thank you so much for that. For the poll generator, I meant that I want my visitors to create their own poll and I want it to be simple for them to do. The other day I started learning MySQL so if that would be easier, you can use MySQL and PHP and JavaScript, though I hear that JavaScript isn't as secure or something...I am not entirely sure, but I guess just use whatever works.

Report •

#3
May 31, 2011 at 22:29:54
✔ Best Answer
No worries; it's an interesting topic and a chance for me to try something too... I've never taken on this sort of thing.

I played around a bit with a crude poll generator; there's no real error checking, but hopefully, it's something that will work with what you're wanting to do. No db code at the moment as I've been quite busy lately.

<html>
<head><?php

   if(isset($_POST["cmd"]) && $_POST["cmd"] === "Create Poll") {

      foreach($_POST as $key => $val) {
         echo $key . " = " . $val . "<br />";
      }

   } else { ?>

<script><!--
 
      MAX_OPTIONS = 10; 

      var url = '<?php echo $_SERVER["PHP_SELF"];?>';
      var tmr = setTimeout('initPoll()', 1500); 
      var noptions; // count of current options

      function initPoll() {

         clearTimeout(tmr);
         document.body.innerHTML = "<input name='new_poll' value='New Poll' type='button' onclick='newPoll();'>" +
                                   "<input name='res_poll' value='Reset' type='button' onclick='resetPoll();'><br /><br />"; 
      }

      function newPoll() {

         noptions = 1;
         document.getElementsByTagName("input")['new_poll'].disabled = 'true';
         
         var d_el = document.createElement("div");
         var txt  = "<input name='add_opt' value='Add Option' type='button' onclick='updatePoll();'>" +                    
                    "<input name='clr_opt' value='Clear Option' type='button' onclick='clearOption();'>" +
                    "<input name='preview' value='Preview Poll' type='button' onclick='previewPoll();'><br /><br />" +                    
                    "Poll Topic: <input style='margin-bottom: 5px' name='topic' type='text' maxlength='120' size='40'><br />" +
                    "Option(s): <input name='poll_options' type='text' maxlength='120' size='35'>"; 

         d_el.id = "div_poll";
         d_el.align = "right";
         d_el.style.width = "450";
         d_el.innerHTML = txt;

         document.body.appendChild(d_el);         

         document.getElementsByTagName("input")['clr_opt'].disabled = true;
         document.getElementsByTagName("input")['topic'].focus();
      }

      function updatePoll() {

         ++noptions;

         if(noptions <= MAX_OPTIONS) {
 
            if(document.getElementsByTagName("input")['clr_opt'].disabled == true)
               document.getElementsByTagName("input")['clr_opt'].disabled = false;

            var i_el = document.createElement("input");

            i_el.name = "poll_options";
            i_el.type = "text";
            i_el.maxLength = "120";
            i_el.size = "35";

            document.getElementsByTagName("div")['div_poll'].appendChild(i_el);
            document.getElementsByName("poll_options")[noptions - 1].focus();

         } else {

            alert('Currently options are limited to ' + MAX_OPTIONS);
            noptions = 10;
         }
      }

      function clearOption() {

         if(noptions > 1) {
            document.getElementsByTagName("div")['div_poll'].
               removeChild(document.getElementsByName("poll_options")[noptions - 1]);
            --noptions;

            if(noptions == 1)
               document.getElementsByTagName("input")['clr_opt'].disabled = true;
         } else if(noptions < 1) {
            noptions = 1;
         }

         document.getElementsByName("poll_options")[noptions - 1].focus();
      }

      function resetPoll() {

         if(document.getElementById("preview_poll") != null && document.getElementById("preview_poll") != undefined)
            document.body.removeChild(document.getElementById("preview_poll"));

         if(document.getElementsByTagName("div")['div_poll'] != null && document.getElementsByTagName("div")['div_poll'] != undefined)
            document.body.removeChild(document.getElementsByTagName("div")['div_poll']);

         document.getElementsByTagName("input")['new_poll'].disabled = false; 
      }

      function previewPoll() {

         // add check for topic / option(s) being blank; if so, handle as desired

         if(document.getElementById("preview_poll") != null && document.getElementById("preview_poll") != undefined)
            document.body.removeChild(document.getElementById("preview_poll"));

         var d_el = document.createElement("div");
         var txt = "<p>Poll Topic: " + document.getElementsByTagName("input")['topic'].value + "</p>" +
                   "<form action='" + url + "' method='post'>" +
                   "<input name='topic' value='" +
                   document.getElementsByTagName("input")['topic'].value +
                   "' type='hidden'>";

         d_el.id = "preview_poll";

         for(n=0; n < document.getElementsByName("poll_options").length; ++n) {
            txt += "<input name='opts' value='" +
                   document.getElementsByName("poll_options")[n].value + 
                   "' type='radio'>" +
                   document.getElementsByName("poll_options")[n].value + "<br />";
         }

         txt += "<input name='cmd' value='Create Poll' type='submit'>" +
                "</form>";

         d_el.innerHTML = txt;
         document.body.appendChild(d_el);
      }

      function savePoll() {
      }

//--></script><?php

   }
?>

</head>
<body>

</body>
</html>


Report •

Related Solutions


Ask Question