[PHP] Captcha

Discussion in 'Code Snippets and Tutorials' started by Josh21227, Jun 16, 2010.

  1. Josh21227

    Josh21227 Level I

    Joined:
    May 23, 2009
    Messages:
    57
    Likes Received:
    10
    Random gradient, random text color, random text length. I also made it rename itself, but I didn't include that in this version.

    I didn't write the creating the gradient part, since I wasn't sure how. Now I know.

    image.php -
    Code (Text):
    1. <?php
    2. header("Content-type: image/png");
    3.  
    4. $height = 45;
    5. $width = 70;
    6.  
    7.  
    8. $start_r = rand(0,255);
    9. $start_g = rand(0,255);
    10. $start_b = rand(0,255);
    11. $end_r = rand(0,255);
    12. $end_g = rand(0,255);
    13. $end_b = rand(0,255);
    14. $image = @imagecreate($width, $height);
    15.  
    16. for($y=0;$y<$height;$y++) {
    17.   for($x=0;$x<$width;$x++) {
    18.     if ($start_r == $end_r) {
    19.       $new_r = $start_r;
    20.     }
    21.     $difference = $start_r - $end_r;
    22.     $new_r = $start_r - intval(($difference / $height) * $y);
    23.     if ($start_g == $end_g) {
    24.       $new_g = $start_g;
    25.     }
    26.     $difference = $start_g - $end_g;
    27.     $new_g = $start_g - intval(($difference / $height) * $y);        
    28.     if ($start_b == $end_b) {
    29.       $new_b = $start_b;
    30.     }
    31.     $difference = $start_b - $end_b;
    32.     $new_b = $start_b - intval(($difference / $height) * $y);
    33.     $row_color = imagecolorresolve($image, $new_r, $new_g, $new_b);
    34.     imagesetpixel($image, $x, $y, $row_color);
    35.   }    
    36. }
    37.  
    38. for ( $counter = 0; $counter <= rand(2,7) ; $counter += 1) {
    39. $choose = rand(1,2);
    40. if($choose == "1") {
    41. $value = $value . chr(rand(48,57));
    42.  
    43. } else {
    44. $value = $value . chr(rand(65,90));
    45.  
    46. }
    47. }
    48.  
    49. setcookie("key", md5(md5($value)));
    50. $text_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    51. imagestring($image, 5, 5, 5, $value, $text_color);
    52.  
    53.  
    54. imagepng($image);
    55. imagedestroy($image);
    56.  
    57. ?>
    58.  
    59.  

    index.php -
    Code (Text):
    1. <?php
    2. if($_GET['key']) {
    3.     if(md5(ms5($_GET['key'])) == $_COOKIE["key"]) {
    4.         echo "Correct code entered";
    5.     } else {
    6.         header( 'Location: index.php?error=1') ;
    7.     }
    8. } else {
    9.     if($_GET['error']){
    10.         echo "Wrong code." . $_GET['error'];
    11.     }
    12. ?>
    13. <form action="index.php" method="get">
    14. Verification:
    15. <input type="text" name="key" /> <img src="image.php">
    16. <input type="submit" value="Submit" />
    17. </form>
    18.  
    19. <?php
    20. }
    21. ?>
    22.  
     
  2. Shawn

    Shawn Level IV

    Joined:
    Jul 15, 2009
    Messages:
    1,989
    Likes Received:
    76
    Location:
    Somewhere, lah.
    Thanks, this might be useful if I know how to implement it.