PHP制作数字字母混合验证码、图片验证码、中文验证码

本文详细介绍如何使用PHP生成数字字母验证码、图片验证码以及中文验证码,并加入干扰点和干扰线提高安全性。此外还介绍了如何刷新验证码图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.制作数字和字母验证码,并添加干扰点、干扰线。
这里写图片描述

实现代码如下:
captcha.php文件:

<?php

session_start();//必须置于顶部启动
// 创建画布
$image = imagecreatetruecolor ( 100, 30 );
$bgcolor = imagecolorallocate ( $image, 160, 200, 180 );
imagefill ( $image, 0, 0, $bgcolor );

// 生成随机数
// for($i=0;$i<4;$i++){
// $fontsize = 6;
// $fontcolor = imagecolorallocate($image, mt_rand(0, 80), mt_rand(50, 100), mt_rand(50, 120));
// $fontcontent = mt_rand(0, 9);

// $x = $i*20+mt_rand(5, 10);
// $y = mt_rand(5, 10);

// imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
// }


$captch_code = "";//空变量
// 生成字母和数字混合
for($i = 0; $i < 4; $i ++) {
    $fontsize = 6;
    $fontcolor = imagecolorallocate ( $image, mt_rand ( 0, 80 ), mt_rand ( 50, 100 ), mt_rand ( 50, 120 ) );

    $data = 'abcdefghijkmnprstuvwxyABCDEFGHIJKLMNPQRSTUVWXY3456789';//除去容易字母数字混淆的元素
    $fontcontent = substr($data, mt_rand(0, strlen($data)),1);

    $captch_code .= $fontcontent;

    $x = $i * 20 + mt_rand ( 5, 10 );
    $y = mt_rand ( 5, 10 );

    imagestring ( $image, $fontsize, $x, $y, $fontcontent, $fontcolor );
}

    $_SESSION['auto_code'] = $captch_code;//保存在session的auto_code变量中

// 增加干扰点
for($i = 0; $i < 200; $i ++) {
    $pointColor = imagecolorallocate ( $image, mt_rand ( 100, 200 ), mt_rand ( 150, 180 ), mt_rand ( 100, 240 ) );
    imagesetpixel ( $image, mt_rand ( 1, 99 ), mt_rand ( 1, 29 ), $pointColor );
}

// 增加干扰线
for($i = 0; $i < 4; $i ++) {
    $lineColor = imagecolorallocate ( $image, mt_rand ( 0, 200 ), mt_rand ( 50, 180 ), mt_rand ( 80, 240 ) );
    imageline ( $image, mt_rand ( 1, 99 ), mt_rand ( 1, 29 ), mt_rand ( 1, 99 ), mt_rand ( 1, 29 ), $lineColor );
}

header ( "content-type:image/png" );
imagepng ( $image );
imagedestroy ( $image );

在实际应用中调用实例:
这里写图片描述

实现代码如下:
form.php文件:

<?php 
    if(isset($_REQUEST['auto_code'])){
        session_start();

        if(strtolower($_REQUEST['auto_code']) == strtolower($_SESSION['auto_code'])){
            echo '输入的验证码正确!';
        }else{
            echo '验证码输入错误,请重新输入!';
        }
        exit();
    }


?>


<html>
<head>
<meta http-equiv="Content-Type" content="tezt/html;charset=utf-8">
<title>验证码校验</title>
</head>
<body>
    <form method="post" action="./form.php">
    <p>验证码图片:
        <img id="captcha_img" alt="显示失败!!!" src="./captcha.php?r=<?php echo rand();?>" width="100" height="30">
        <a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='./captcha.php?r'+Math.random()">换一张</a>
    </p>
    <p>请输入验证码信息:<input type="text" name="auto_code" value=""></p>
    <p><input type="submit" value="提交"></p>
    </form>
</body>
</html>

2.制作图片验证码,示例如下:
这里写图片描述

captcha_img.php文件:

<?php

    session_start();

    $table = array(
        'pic1'=>'兔',
        'pic2'=>'猫',
        'pic3'=>'老虎',
        'pic4'=>'熊猫',   
    );

    $index = rand(1,4);

    $value=$table['pic'.$index];
    $_SESSION['auto_code']=$value;

    $filename=dirname(__FILE__).'\\images\pic'.$index.'.jpg';//这里路径一定要写对
    $contents=file_get_contents($filename);

    ob_clean();
    header('content-type:image/jpg');
    echo $contents;

在表单中调用:
form_img.php文件:

<?php

    session_start();

    $table = array(
        'pic1'=>'兔',
        'pic2'=>'猫',
        'pic3'=>'老虎',
        'pic4'=>'熊猫',   
    );

    $index = rand(1,4);

    $value=$table['pic'.$index];
    $_SESSION['auto_code']=$value;

    $filename=dirname(__FILE__).'\\images\pic'.$index.'.jpg';//这里路径一定要写对
    $contents=file_get_contents($filename);

    ob_clean();
    header('content-type:image/jpg');
    echo $contents;

3.制作中文验证码
这里写图片描述

制作思路同上,就不一一赘述了。
captcha_cn.php文件

<?php

// error_reporting(E_ALL & ~E_NOTICE);
session_start();//必须置于顶部启动
// 创建画布
$image = imagecreatetruecolor ( 200, 60 );
$bgcolor = imagecolorallocate ( $image, 160, 200, 180 );
imagefill ( $image, 0, 0, $bgcolor );

$fontface='SIMHEI.TTF';

$str="我们都是打快点打算考虑到觉觉得授课是肯定很快才能对你发法师法基安抚发生了开发";
$strdb=str_split(substr($str, 3),3);//每三个字符一个汉字/一个汉字三个字符
// $strdb = array('天');
// $strdb=iconv("gbk","utf-8", "天马行空");
$captch_code='';
// 生成字母和数字混合
for($i = 0; $i < 4; $i ++) {
//  $cn=$strdb[$i];
    $index=mt_rand(0, count($strdb));
    $cn=$strdb[$index];
    echo $cn;
    $captch_code.=$cn;

    $size=mt_rand(5, 20);
    $angle=mt_rand(-25, 25);
    $x=$i*40+20;
    $y=mt_rand(25, 35);
    $fontcolor = imagecolorallocate ( $image, mt_rand ( 0, 80 ), mt_rand ( 50, 100 ), mt_rand ( 50, 120 ) );

    imagettftext($image, $size, $angle, $x, $y, $fontcolor,$fontface, $cn);
}
    $_SESSION['auto_code'] = $captch_code;//保存在session的auto_code变量中

// 增加干扰点
for($i = 0; $i < 200; $i ++) {
    $pointColor = imagecolorallocate ( $image, mt_rand ( 100, 200 ), mt_rand ( 150, 180 ), mt_rand ( 100, 240 ) );
    imagesetpixel ( $image, mt_rand ( 1, 199 ), mt_rand ( 1, 59 ), $pointColor );
}

// 增加干扰线
for($i = 0; $i < 4; $i ++) {
    $lineColor = imagecolorallocate ( $image, mt_rand ( 0, 200 ), mt_rand ( 50, 180 ), mt_rand ( 80, 240 ) );
    imageline ( $image, mt_rand ( 1, 199 ), mt_rand ( 1, 59 ), mt_rand ( 1, 199 ), mt_rand ( 1, 59 ), $lineColor );
}
ob_clean();
header ( "Content-Type: image/png");
imagepng ( $image );
imagedestroy ( $image );

form_cn.php文件:

<?php 
    if(isset($_REQUEST['auto_code'])){
        session_start();

        if(strtolower($_REQUEST['auto_code']) == strtolower($_SESSION['auto_code'])){
            echo '输入的验证码正确!';
        }else{
            echo '验证码输入错误,请重新输入!';
        }
        exit();
    }


?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta charset="utf-8">
<title>验证码校验</title>
</head>
<body>
    <form method="post" action="./form.php">
    <p>验证码图片:
        <img id="captcha_img" alt="显示失败!!!" src="./captcha_cn.php?r=<?php echo rand();?>" width="200" height="60">
        <a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='./captcha_cn.php?r'+Math.random()">换一张</a>
    </p>
    <p>请输入验证码信息:<input type="text" name="auto_code" value=""></p>
    <p><input type="submit" value="提交"></p>
    </form>
</body>
</html>

哈哈~~以上三种验证码就只做成功了,再学习下图片水印。

智能网联汽车的安全员高级考试涉及多个方面的专业知识,包括但不限于自动驾驶技术原理、车辆传感器融合、网络安全防护以及法律法规等内容。以下是针对该主题的一些核心知识解析: ### 关于智能网联车安全员高级考试的核心内容 #### 1. 自动驾驶分级标准 国际自动机工程师学会(SAE International)定义了六个级别的自动驾驶等级,从L0到L5[^1]。其中,L3及以上级别需要安全员具备更高的应急处理能力。 #### 2. 车辆感知系统的组成与功能 智能网联车通常配备多种传感器,如激光雷达、毫米波雷达、摄像头和超声波传感器等。这些设备协同工作以实现环境感知、障碍物检测等功能[^2]。 #### 3. 数据通信与网络安全 智能网联车依赖V2X(Vehicle-to-Everything)技术进行数据交换,在此过程中需防范潜在的网络攻击风险,例如中间人攻击或恶意软件入侵[^3]。 #### 4. 法律法规要求 不同国家和地区对于无人驾驶测试及运营有着严格的规定,考生应熟悉当地交通法典中有关自动化驾驶部分的具体条款[^4]。 ```python # 示例代码:模拟简单决策逻辑 def decide_action(sensor_data): if sensor_data['obstacle'] and not sensor_data['emergency']: return 'slow_down' elif sensor_data['pedestrian_crossing']: return 'stop_and_yield' else: return 'continue_driving' example_input = {'obstacle': True, 'emergency': False, 'pedestrian_crossing': False} action = decide_action(example_input) print(f"Action to take: {action}") ``` 需要注意的是,“同学”作为特定平台上的学习资源名称,并不提供官方认证的标准答案集;建议通过正规渠道获取教材并参加培训课程来准备此类资格认证考试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值