Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Web languages(HTML, CSS, JS, jQuery, etc.)] [PHP] Create a Captcha images & form
#1
Heres a little release i am giving out, just copy the code and paste it into your public_html.

index.php
Code:
<?php
session_start();

if ( isset( $_POST['submit'] ) )
{
$code = strip_tags( $_POST['code'] );
$real_code = $_SESSION['captcha_code'];
if ( $code !== $real_code )
{
echo "Wrong code! Code was {$real_code}";
}
else
{
echo "Correct code!";
}
}
else
{
?>
<form method="post">
<img src="captcha.php" /><br />
Enter code: <input type="text" name="code" /> <input type="submit" name="submit" value="Go" />
</form>
<?php
}
?>
captcha.php
Code:
<?php
/* This tutorial will teach you how to create your own Captcha image. */

session_start();
$chars = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
$length = ( isset( $_GET['l'] ) && is_numeric( $_GET['l'] ) ) ? (int)$_GET['l'] : 8;

$captcha = "";
for ( $i = 1; $i <= $length; $i++ )
{
$captcha .= $chars[ mt_rand( 0, strlen( $chars ) ) ];
}

$_SESSION['captcha_code'] = $captcha;

$width = ( $length * 10 ) - 3;
$img = imagecreate( $width, 17 );
$bg = imagecolorallocate( $img, 0, 0, 0 ); //black
$txt = imagecolorallocate( $img, 255, 255, 255 ); //white
imagestring( $img, 5, 3, 1, $captcha, $txt );

header( "Content-Type: image/PNG" );
imagepng( $img );
imagedestroy( $img );
?>

Hope you enjoyed the release!

Thanks,
Derpec
Reply
#2
Nice tutorial thanks so much!
Kind regards, Trojan.
Reply
#3
Nice Tutorial will check it out thanks.
Reply
#4
This is a nice tutorial for someone who is learning PHP.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)