显示root之外的验证码

时间:2013-08-29 15:11:03

标签: php captcha

我无法在共享服务器中显示位于root之外的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?它是安全的吗?有什么更好的方法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

您需要提供外部可访问的地址/域而不是

<?=ROOT_PATH.$this->editPath;?>

类似

$mydomain = 'http://www.yourdomain.com';
<?=$mydomain;?>function/security_code.php?<?=mt_rand(100, 9999);?>

IMHO。 href以这种方式工作。