Code isn't working .. ( simple bot check )

Discussion in 'PHP' started by Gravereaper, Jan 5, 2009.

  1. #1
    I was testing out this code but its not working ... someone please check it out

    <?
    srand(time());
    $random = (rand()%0);
    print("number: $random");
    
        echo "<form action='random.php' method='post'>\n
              <input type='text' name='pass' />\n
              </br><input type='submit' value='POST' />\n
              </form>\n";   
    
    
    
    if( $_POST['pass'] == $random )
    {  $datastring = "Its working for ".$random;
      $fo = fopen('rec.txt','a');
      if(!$fo)
      {
        die("Cannot open $et"); 
      }
      if(!fwrite($fo,$datastring))
      {
        die("Cannot write to $et"); 
      }
      fclose($fo);
    }
    
    ?>
    Code (markup):

     
    Gravereaper, Jan 5, 2009 IP
  2. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    what error did you get?
     
    javaongsan, Jan 5, 2009 IP
  3. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    none, it should write something on the text files and its not
     
    Gravereaper, Jan 5, 2009 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    use file_put_contents() for file writing.
     
    Kaizoku, Jan 5, 2009 IP
  5. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    is the file chmodded 0777?
     
    atlantaazfinest, Jan 5, 2009 IP
  6. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I guess the if condition is comparing the previous number entered with the latest one... correct me if i am wrong?
     
    Gravereaper, Jan 5, 2009 IP
  7. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #7
    Your logic seems invalid.

    How do you know which number will be generated? When you submit the form, there will be another number generated. So your last entered number must match the new number that appears after submit.

    Peace,
     
    Barti1987, Jan 5, 2009 IP
  8. chmdznr

    chmdznr Active Member

    Messages:
    417
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #8
    Try this..
    
    <?
    if ($_POST["pass"]=="") {
    srand(time());
    $random = (rand()%0);
    print("number: $random");
    }
        echo "<form action='random.php' method='post'>\n
              <input type='text' name='pass' />\n
              </br><input type='submit' value='POST' />\n
              </form>\n";   
    
    
    
    if( $_POST['pass'] == $random )
    {  $datastring = "Its working for ".$random;
      $fo = fopen('rec.txt','a');
      if(!$fo)
      {
        die("Cannot open $et"); 
      }
      if(!fwrite($fo,$datastring))
      {
        die("Cannot write to $et"); 
      }
      fclose($fo);
    }
    
    ?>
    
    Code (markup):
     
    chmdznr, Jan 5, 2009 IP
  9. Th3DarkOn3

    Th3DarkOn3 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I'm not sure if the previous response would work, because I don't believe $random would be assigned at all on a form posting.... To the best of my knowledge, if $random is assigned prior to the post, then the form was posted, since the section assigning a value to $random is bypassed upon form submission, $random would be unassigned....

    Let me know if this works for you .......

    <?
    srand(time());
    $random = (rand()%0);
    print("number: $random");
    
        echo "<form action='random.php' method='post'>\n
              <input type='text' name='pass' />\n
              </br><input type='submit' value='POST' />\n
              <input type='hidden' name='hidRandom' value='$random' />\n
              </form>\n";   
    
    
    
    if( $_POST['pass'] == $_POST['hidRandom'])
    {  $datastring = "Its working for ".$random;
      $fo = fopen('rec.txt','a');
      if(!$fo)
      {
        die("Cannot open $et"); 
      }
    else { echo "Was able to open $et<br />"; }
      if(!fwrite($fo,$datastring))
      {
        die("Cannot write to $et"); 
      }
    else { echo "Was able to write to $et<br />"; }
      fclose($fo);
    }
    
    ?>
    Code (markup):
    That way, you assign a hidden variable in your form to hold the randomly created value. When you post the form, you check the user-entered value against the hidden field created when the user first went to the form. Then, i added the "else" lines just to check and make sure that your fOpen and fWrite's are working .....

    Let me know if that workes.
     
    Th3DarkOn3, Jan 5, 2009 IP
  10. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #10
    Wow, this many posts and not a single person has pointed out this?

    $random = (rand()%0);
    PHP:
    Perhaps this needs emphasis... YOU CANNOT DIVIDE BY ZERO.

    No random number is being generated at all, a warning would be triggered if you had warnings set to on in error_reporting.

    [if( $_POST['pass'] == $random )
    PHP:
    Is simply never going to match a number.. rand() % 0 returns (bool) false!

    Dan.
     
    Danltn, Jan 5, 2009 IP
  11. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Actually I've tested it with %99999 as well .. nothing, please read my last reply maybe you'll get some hints?
     
    Gravereaper, Jan 6, 2009 IP
  12. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #12
    Why are you mod'ing it anyway, and why are you bothering to seed the random number generator? That's unnecessary since PHP 4.2.
     
    Danltn, Jan 7, 2009 IP
  13. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Danltn I used sessions to fix it out :) ..

    thanks everyone
     
    Gravereaper, Jan 7, 2009 IP