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):
I guess the if condition is comparing the previous number entered with the latest one... correct me if i am wrong?
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,
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):
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.
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.
Actually I've tested it with %99999 as well .. nothing, please read my last reply maybe you'll get some hints?
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.