为什么我的php代码显示不出来验证码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
</head>
<body background="../c4d4e0dce3b544b58443019a511f51f0.jpg" style="background-size: 100% 100%;background-attachment: fixed;">
    <div class="box">
        <div class="title">用户登录</div>
        <form action="../login.php" method="post">
            <table class="login">
                <tr><th>用户名:</th><td><input type="text" name="username" /></td></tr>
                <tr><th>密码:</th><td><input type="password" name="password" /></td></tr>
                <tr><th>验证码:</th><td><input type="text" name="captcha" /></td></tr>
                <tr><th></th><td><img src="../view/code.php" /></td></tr>
                <tr><th></th><td><input type="submit" value="登录"/><a href="register.php"><input type="button" value="前往注册"></a></td>
                </tr>
            </table>
        </form>
    </div>
    
</body>
</html>
<?php
//创建验证码
//验证码画布
$img_w = 100;
$img_h = 30;
$img = imagecreatetruecolor($img_w, $img_h); //创建画布
$bg_color = imagecolorallocate($img,0xcc,0xcc,0xcc); //画布颜色
imagefill($img,0,0,$bg_color); //背景色

//生成验证码文本
$count = 4;
$charset = 'ABCDEFGHIJKLMNOBQRXTUVWXYZ23456789'; //随机数
$charset_len = strlen($charset) -1; //随机数长度
$code=''; //保存生成的验证码
for ($i = 0; $i < $count; ++$i) {
    //通过索引取出字符 mt_rand()用于获取指定范围内的随机数
    $code .= $charset[mt_rand(0, $charset_len)];
}

//将生成的文本保存到session
session_start(); //启动session
$_SESSION['$capcha'] = $code;

//在画布中生成验证码文本
$fontsize = 16; //文字大小
$fontstyle = 'C:\Windows\Fonts\PRISTINA.TTF'; //字体样式
//指定长度的验证码
for($i=0; $i<$sount; ++$i) {
    //随机生成字体颜色
    $fontcolor = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,50),mt_rand(0,255));
    imagettftext(
        $img, //画布资源
        $fontsize, //文字大小
        mt_rand(0,20) - mt_rand(0,25), //随机设置文字倾斜角度
        $fontsize*$i+20,mt_rand($img_h,$img_w), //随机设置文字坐标,并自动计算间距
        $fontcolor, //字体颜色
        $fontstyle, //文字字体
        $code[$i] //文字内容
    );
}

//为验证码图片生成彩色噪点
for($i=0; $i<300; ++$i){
    //随机生成颜色
    $color=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    //绘制随机干扰点
    imagesetpixel($img,mt_rand(0,$img_w),mt_rand(0,$img_h),$color);
}

//绘制干扰线
for($i=0; $i<10;++$i){
    //随机生成干扰线颜色
    $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    //随机绘制干扰线
    imageline($img,mt_rand(0,$img_w),0,mt_rand(0,$img_h*5),$img_h,$color);
}

header('content-Type: image/gif'); //输出图像
imagegif($img);

有大佬知道吗

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值