-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from devplanete/patch-16
Update showCaptcha.php: code optimisation + check php gd extension
- Loading branch information
Showing
1 changed file
with
30 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,74 @@ | ||
<?php | ||
|
||
/** | ||
* a little bit dirty, but it works: | ||
* This file generates a captcha string, writes it into the $_SESSION['captcha'] | ||
* and renders a fresh captcha graphic file to the browser. This is oldschool-non-PHP, | ||
* but it does the job. | ||
* and renders a fresh captcha graphic file to the browser. | ||
* | ||
* In the views you can use this by saying: | ||
* <img src="tools/showCaptcha.php" /> | ||
* | ||
* Check if the typed captcha is correct by saying: | ||
* if ($_POST["captcha"] == $_SESSION['captcha']) { ... } else { ... } | ||
* | ||
*/ | ||
|
||
// check if php gd extension is loaded | ||
if (!extension_loaded('gd')) { | ||
die("It looks like GD is not installed"); | ||
} | ||
|
||
session_start(); | ||
|
||
// create set of usage characters | ||
$letters = array_merge( range('A', 'Z') , range(2, 9) ); | ||
unset($letters[array_search('O', $letters)]); | ||
unset($letters[array_search('Q', $letters)]); | ||
unset($letters[array_search('I', $letters)]); | ||
unset($letters[array_search('5', $letters)]); | ||
unset($letters[array_search('S', $letters)]); | ||
shuffle($letters); | ||
$selected_letters = array_slice($letters, 0, 4); | ||
$secure_text = implode('', $selected_letters); | ||
// target captcha string length | ||
$iCaptchaLength = 4; | ||
|
||
// following letters are excluded from captcha: O, Q, I, 5, S | ||
$str_choice = 'ABCDEFGHJKLMNPRTUVWXYZ2346789'; | ||
$str_captcha = ''; | ||
// create target captcha with letters comming from $str_choice | ||
for ($i=0; $i < $iCaptchaLength; $i++) { | ||
do { | ||
$ipos = rand(0, strlen($str_choice) - 1); | ||
// checks that each letter is used only once | ||
} while (stripos($str_captcha, $str_choice[$ipos]) !== false); | ||
|
||
// write the 4 selected letters into a SESSION variable | ||
$_SESSION['captcha'] = $secure_text; | ||
$str_captcha .= $str_choice[$ipos]; | ||
} | ||
|
||
// get letters from SESSION, split them, create array of letters | ||
$letters = str_split($_SESSION['captcha']); | ||
// write the captcha into a SESSION variable | ||
$_SESSION['captcha'] = $str_captcha; | ||
|
||
// begin to create the image with PHP's GD tools | ||
$im = imagecreatetruecolor(150, 70); | ||
// TODO: error handling if creating images fails | ||
//or die("Cannot Initialize new GD image stream"); | ||
|
||
$bg = imagecolorallocate($im, 255, 255, 255); | ||
imagefill($im, 0, 0, $bg); | ||
|
||
// create background with 1000 short lines | ||
/* | ||
for($i=0;$i<1000;$i++) { | ||
/*for($i=0;$i<1000;$i++) { | ||
$lines = imagecolorallocate($im, rand(200, 220), rand(200, 220), rand(200, 220)); | ||
$start_x = rand(0,150); | ||
$start_y = rand(0,70); | ||
$end_x = $start_x + rand(0,5); | ||
$end_y = $start_y + rand(0,5); | ||
imageline($im, $start_x, $start_y, $end_x, $end_y, $lines); | ||
} | ||
*/ | ||
}*/ | ||
|
||
// create letters. for more info on how this works, please | ||
// @see php.net/manual/en/function.imagefttext.php | ||
// TODO: put the font path into the config | ||
$i = 0; | ||
foreach ($letters as $letter) { | ||
$text_color = imagecolorallocate($im, rand(0,100), rand(10,100), rand(0,100)); | ||
for ($i=0; $i < $iCaptchaLength; $i++) { | ||
$text_color = imagecolorallocate($im, rand(0, 100), rand(10, 100), rand(0, 100)); | ||
// font-path relative to this file | ||
imagefttext($im, 35, rand(-10, 10), 20+($i*30) + rand(-5, +5), 35 + rand(10, 30), $text_color, 'fonts/times_new_yorker.ttf', $letter); | ||
$i++; | ||
imagefttext($im, 35, rand(-10, 10), 20 + ($i * 30) + rand(-5, +5), 35 + rand(10, 30), $text_color, 'fonts/times_new_yorker.ttf', $str_captcha[$i]); | ||
} | ||
|
||
// send http-header to prevent image caching (so we always see a fresh captcha image) | ||
header('Content-type: image/png'); | ||
header('Pragma: no-cache'); | ||
header('Cache-Control: no-store, no-cache, proxy-revalidate'); | ||
|
||
// send image to browser, destroy image from php "cache" | ||
// send image to browser and destroy image from php "cache" | ||
imagepng($im); | ||
imagedestroy($im); | ||
|
||
?> |