Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 389
0 Users 3 Guests Online

Creating a CAPTCHA

It's a fairly long code, so I'll explain it afterwards.

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
session_start
();
$alpanumb "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstufwxyz1234567890";
$code substr(str_shuffle($alpanumb), 05);
$num "1230";
$rand substr(str_shuffle($num), 02);
$coded base64_encode($code);
$_SESSION['CAPTCHA'] = $code;
$width 118;
$height 27;

$im imagecreatefrompng("./captcha.PNG");
$black imagecolorallocate($imrand(1255), rand(1255), rand(1255));

// grid
$grid_color imagecolorallocate($im17500);
$number_to_loop ceil($width 20);
for(
$i 0$i $number_to_loop$i++) {
    
$x = ($i 1) * 20;
    
imageline($im$x0$x$height$grid_color);
}
$number_to_loop ceil($height 10);
for(
$i 0$i $number_to_loop$i++) {
    
$y = ($i 1) * 10;
    
imageline($im0$y$width$y$grid_color);
}

// random lines
$line_color imagecolorallocate($im000);
for(
$i 0$i 30$i++) {
    
$rand_x_1 rand(0$width 1);
    
$rand_x_2 rand(0$width 1);
    
$rand_y_1 rand(0$height 1);
    
$rand_y_2 rand(0$height 1);
    
imageline($im$rand_x_1$rand_y_1$rand_x_2$rand_y_2$line_color);
}

imagestring($im5$rand10$code$black);
header("Content-type: image/png");
header("no-cache");
imagepng($im);
echo 
"<BR><BR>$random1";
?>


Alrighty then, you can call that whatever you wish.

-----------------------HOW TO USE-----------------------

**PLEASE NOTE: You must create your own image or use the one below:

TutorialNinja Image

Just recolour it to your needs. To show the image, all you must do it on your page (not the php page) put this where you want it:

Code
<img src="captcha.php">


Just change captcha with the file name. Sometimes the font color is not readable (or at least for mine) so instead change the above image code to:

Code
<img src="../captcha.php" onclick="this.src='../captcha.php?time=' + (new Date()).getTime();">


-----------------------ABOUT-----------------------

Pretty self explanatory really, everything is in English. But, I'll explain the most important things.

$im, specifies the image, allowing modifications to happen.
$black, specifies the font colour, don't know why it's called black.

That's basically it. Nothing more too it. Hope this helps!
Diablosblizz
Views:
2525
Rating:
Posted on Sunday 28th September 2008 at 05:56 AM
Nathan
Nathan
nope still dont work
Posted on Sunday 28th September 2008 at 05:46 AM
ShadowMage
ShadowMage
remove session_start(); from the top? or maybe the echo at the bottom.
Posted on Sunday 28th September 2008 at 04:57 AM
Nathan
Nathan
Warning: imagecreatefrompng(./usersystem/captcha.PNG) [function.imagecreatefrompng]: failed to open stream: No such file or directory in /home/rdlegend/public_html/usersystem/captcha.php on line 12

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 13

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 16

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 20

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 25

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 25

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 25

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 29

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imageline(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 35

Warning: imagestring(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 38

Warning: Cannot modify header information - headers already sent by (output started at /home/rdlegend/public_html/usersystem/captcha.php:12) in /home/rdlegend/public_html/usersystem/captcha.php on line 39

Warning: Cannot modify header information - headers already sent by (output started at /home/rdlegend/public_html/usersystem/captcha.php:12) in /home/rdlegend/public_html/usersystem/captcha.php on line 40

Warning: imagepng(): supplied argument is not a valid Image resource in /home/rdlegend/public_html/usersystem/captcha.php on line 41
Posted on Wednesday 9th April 2008 at 06:13 PM
new2old
new2old
Or even better change

Code

<img src="captcha.php">


to

Code

<img src="captcha.PNG">


and

Code

<img src="../captcha.php" onclick="this.src='../captcha.php?time=' + (new Date()).getTime();">


to

Code

<img src="../captcha.PNG" onclick="this.src='../captcha.php?time=' + (new Date()).getTime();">


;)
Posted on Wednesday 9th April 2008 at 03:17 PM
DanielXP
DanielXP
And the header

header("Content-type: image/png");
Posted on Wednesday 9th April 2008 at 02:29 PM
new2old
new2old
PNG's dont for some hosts, change it to JPG using photoshop so you dont lose and picture quality

Then change


$im = imagecreatefrompng("./captcha.PNG");

to


$im = imagecreatefrompng("./captcha.jpg");
Posted on Monday 7th April 2008 at 02:06 PM
DanielXP
DanielXP
Comments on the code?
Posted on Sunday 6th April 2008 at 09:07 PM
Diablosblizz
Diablosblizz
Hmm, the image didn't show:

http://clan-ik.frih.net/captcha.PNG

Sorry.