Jump to content

valadating random number


redarrow

Recommended Posts


Got any idears why this wont work please cheers.


<?php

$list = array("1","2","3","4","5","6","7","8","9","0",);

for ($i = 1; $i <= 6; $i++) {
$generated_code .= $list[rand(0,9)];

}

echo $generated_code;




$crack=($_POST['crack']);



if(!$_POST['submit']){

if($crack==$generated_code){

echo"You entered the correct code!";

}else{

echo "incorrect code!";

}

}

?>

<html>
<body>
<form method="post" action="">
<input type="text" name="crack">
<input type="submit" value="send">
</form>
</html>
</body>
Link to comment
https://forums.phpfreaks.com/topic/13366-valadating-random-number/
Share on other sites

[code]
<?php

$list = array( "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" );

for( $i = 1; $i <= 6; $i++ ) {
      $rand = rand( 0, 9 );
      $generated_code .= $list[$rand];
}

echo $generated_code;

if( $_POST['submit'] )
{
      $crack= $_POST['crack'];

      if( $crack == $generated_code )
      {
            echo "You entered the correct code!";
      }
      else
      {
            echo "incorrect code!";
      }
}

?>

<html>
<body>
<form method="post" action="">
<input type="text" name="crack">
<input type="submit" value="send">
</form>
</body>
</html>[/code]
[code]<?php

$list = array( "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" );

for( $i = 1; $i <= 6; $i++ ) {
      $rand = rand( 0, 9 );
      $generated_code .= $list[$rand];
}

echo $generated_code;

if( $_POST[validate] )
{
      $crack = $_POST[crack];

      if( $crack == $generated_code )
      {
            echo "<br><br>You entered the correct code!";
      }
      else
      {
            echo "<br><br>incorrect code!";
      }
}

?>

<html>
<body>
<form method="post">
<input type="text" name="crack">
<input type="submit" name="validate" value="send">
</form>
</body>
</html>
[/code]

you didnt name the submit button
redarrow your code doesn't work because as was mentioned by whale, you did not name your submit button, and also you only set your script to check if they codes match if the user has not clicked on submit button yet. Remove the ! and name your submit button

if ([color=red]![/color]$_POST['submit']) { ... }

and

<input type="submit" value="send" [color=red]name='submit'[/color]>

this is the code in the link try


<?php

$list = array( "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" );

for( $i = 1; $i <= 6; $i++ ) {
     $rand = rand( 0, 9 );
     $generated_code .= $list[$rand];
}

echo $generated_code;

if( $_POST[validate] )
{
     $crack = $_POST[crack];

     if( $crack == $generated_code )
     {
           echo "<br><br>You entered the correct code!";
     }
     else
     {
           echo "<br><br>incorrect code!";
     }
}

?>

<html>
<body>
<form method="post">
<input type="text" name="crack">
<input type="submit" name="validate" value="send">
</form>
</body>
</html>
change it to this:
[code]
<?php
if($_POST[validate] ) {
      $crack = $_POST[crack];

      if( $crack == $generated_code )
      {
            echo "

You entered the correct code!";
      }
      else
      {
            echo "

incorrect code!";
      }
} else {
$list = array( "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" );

for( $i = 1; $i <= 6; $i++ ) {
      $rand = rand( 0, 9 );
      $generated_code .= $list[$rand];
}

echo $generated_code;

}


?>

<html>
<body>
<form method="post">
<input type="text" name="crack">
<input type="submit" name="validate" value="send">
</form>
</body>
</html>
[/code]
I will explain the method you are doing:

1. you generate the code
2. you submit a code
3. a new code is generated
4. the submited code checks if it is the same as the new generated code (this is different to the number echoed before the form)
5. you got like a billion to 1 chance of guessing the number.. lol
[quote author=redarrow link=topic=99080.msg390072#msg390072 date=1151744878]
can it be solved with using sessions i wonder.
[/quote]

easiest way to do it is do a query to insert into a table which stores the users ip address and the random number.

then when you submit it checks the database for the code and the same ip address

if no match is found it echos wrong, otherwise correct
okay revised code:
[code]
<?php
if($_POST['validate'] ) {
      $crack = $_POST['crack'];
      $generated_code = $_POST['generated_code'];
      if( $crack == $generated_code )
      {
            echo "

You entered the correct code!";
      }
      else
      {
            echo "

incorrect code!";
      }
} else {
$list = array( "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" );

for( $i = 1; $i <= 6; $i++ ) {
      $rand = rand( 0, 9 );
      $generated_code .= $list[$rand];
}

echo $generated_code;

}


?>
<html>
<body>
<form method="post">
<input type="text" name="crack">
<input type='hidden' name='generated_code' value='<?= $generated_code ?>'>
<input type="submit" name="validate" value="send">
</form>
</body>
</html>

[/code]
Crayon Violent 

the code you posted is so good but becouse the value matches the echo success is always lol

brain teaser man lol............


there is a way to get a generated number to match via a form on one page but how sessions maybe
dont no but does exist promise
[quote author=mrwhale link=topic=99080.msg390075#msg390075 date=1151745078]
[quote author=redarrow link=topic=99080.msg390072#msg390072 date=1151744878]
can it be solved with using sessions i wonder.
[/quote]

easiest way to do it is do a query to insert into a table which stores the users ip address and the random number.

then when you submit it checks the database for the code and the same ip address

if no match is found it echos wrong, otherwise correct
[/quote]


bingo ;) not reading my posts :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.