captcha
共有サーバーのルート外にあると表示できません。マイhtml
ページ:
<img align="top" id="captcha" src="<?=ROOT_PATH.$this->editPath;?>function/security_code.php?<?=mt_rand(100, 9999);?>" />
で正常に動作しlocalhost
ます。
とsecurity_code.php
は:
header('Content-Type: image/jpeg');
session_start();
class captchaGD {
public $font='arial.ttf';
public function generateCode($characters) {
$possible = '23456789bcdfghjkmnpqrstvwxyzBCDFGHJKMNPQRSTVWXYZ';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}
function __construct($width='120',$height='40',$characters='6') {
$code = $this->generateCode($characters);
$font_size = $height * 0.5;
$image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
for( $i=0; $i<($width*$height)/200; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
imagejpeg($image);
imagedestroy($image);
$_SESSION['security_code']['form']="";
$_SESSION['security_code']['form'] = strtolower($code);
}
}
$width = '120';
$height = '40';
$characters = '3';
$captcha = new captchaGD($width,$height,$characters,$_SERVER['REQUEST_URI']);
編集security_code.php
:コメントによると、入れたらうまくいきpublic_html
ますか?それは安全な方法ですか?これを行うより良い方法は?