ホームページ  >  記事  >  バックエンド開発  >  PHPで算術検証コードを実装する方法

PHPで算術検証コードを実装する方法

不言
不言オリジナル
2018-07-05 15:04:562375ブラウズ

この記事では、主に PHP で算術検証コードを実装する方法を紹介します。これには、一定の参考値があります。今、あなたに共有します。困っている友達は、それを参照できます。

私は最近、 PHP を学びました 画像関数と検証コードの実装も練習しました 先生のアドバイスに従って、算術検証コード関数を書きました 初めて単独で小さな演習を完了しました とてもうれしかったです しかし、私はまた、そこで、あらゆる分野の専門家が私にガイダンスを与え、私の視野をさらに広げることができることを願って、具体的に書きました

#PHP コードは次のとおりです: 検証コード

<?php/**
 * @param int $width 宽度,默认为120
 * @param int $height 高度,默认为50
 * @param int $fontSize 字体的大小
 * @return 图片资源
 */function arithmeticCode($width=120,$height=50,$fontSize=20){
    //开启session
    session_start();    //创建画布
    $img = imagecreatetruecolor($width,$height);    //分配颜色
    $color = imagecolorallocate($img,255,255,255);    //填充颜色
    imagefill($img,0,0,$color);    //干扰点
    for ($i = 0;$i < 500;$i++){        $pixColor = imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
        imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$pixColor);
    }    //干扰线
    for ($i = 0;$i < 4;$i++){        $lineColor = imagecolorallocate($img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
        imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$lineColor);
    }    //定义一个数组存放运算符号
    $arr = [&#39;+&#39;,&#39;-&#39;,&#39;*&#39;];    //计算数组的长度
    $len = count($arr);    //定义一个1到20的数组
    $num = range(1,20);    $numLen = count($num);    //定义一个空数组来存放随机取得的验证码
    $code = [];    for ($i = 0;$i < $len;$i++) {        if ($i == 1) {            $code[] = $arr[mt_rand(0,$len-1)];
        }else {            $code[] = $num[mt_rand(0,$numLen-1)];
        }
    }    $str = implode($code);//将数组转为字符串
    $textColor = imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));    $fontAngle = 0;    $x = ($width - $fontSize*3)/2;    $y = ($height - $fontSize) / 2 + $fontSize;
    imagettftext($img,$fontSize,$fontAngle,$x,$y,$textColor,"./img/msyh.ttc",$str);    $res = getRes($code);    //将函数存放在session中
    $_SESSION[&#39;res&#39;] = $res;    //输出图片
    header("content-type:image/png");
    imagepng($img);
}/**
 * @param $arr 一个包含运算符号的数组
 * @return 返回一个运算结果
 */function getRes($arr) {
    $sum = 0;    //判断数组元素下标为1的运算符号是什么
    switch ($arr[1]){        case &#39;+&#39;:            $sum = $arr[0] + $arr[2];            break;        case &#39;-&#39;:            $sum = $arr[0] - $arr[2];            break;        case &#39;*&#39;:            $sum = $arr[0] * $arr[2];            break;
    }    return $sum;
}//调用函数arithmeticCode(100,40,18);
#の機能を実装

##htmlのコードの一部

<!doctype html><html lang="en"><head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        img {            position: relative;            top: 20px;        }
    </style></head><body><form action="test.php">
    验证码 <input type="text" name="code">
    <img src="./demo5.php" alt="点击刷新">
    <br>
    <button>提交</button></form></body></html><script>
    //实现点击图片刷新验证码的功能
    var img = document.querySelector("img");
    img.onclick = function () {
        this.src = this.src+"?m="+Math.random();
    }</script>

test.phpのテスト

<?phpsession_start();$res = $_SESSION[&#39;res&#39;];$value = $_GET[&#39;code&#39;];if ($res == $value) {    echo "test success";
}else{    echo "test fail";
}

上記は私のコードの構成です。

皆さんもできるといいですね 大神様が教えて下さる【顔が楽しみです】

以上がこの記事の全内容です. 皆様の学習にお役に立てれば幸いです。関連コンテンツの詳細については、PHP 中国語 Web サイトにご注目ください。

関連する推奨事項:

PHP7 での MongoDB ドライバーの使用の概要

配列内の値を一連のセットに代入するPHP メソッドの変数

以上がPHPで算術検証コードを実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。