• PHP图形图像的典型应用 常用图像的应用(验证码)


    php生成动态的验证码,是php防止恶意登陆或者注册等常规手段-废话不多说,直接看例子。(只是一个简单的应用,如果要安全或者更复杂的,请期待我以后的文章)

    PHP生成验证码核心文件 (checks.php):

    <?php
        /*成生随机的验证码。此实例只是一个简单的例子,如果不想被人识别,还可以加噪点等干扰技术*/
        session_start(); //启动session
        header("Content-type:image/x-png"); //设置创建图像的格式
        $image_width = 70;  //设置图像的宽度
        $image_height = 18;  //设置图像的高度
        srand(microtime()*100000);  //设置随机数的种子。 --这里关于srand和microtime函数请自行查阅php手册
        for($i=0; $i<4; $i++)    //循环输出一个4位数的随机数
        {
            $new_number.=dechex(rand(0,15)); //将十进制随机数转为十六进制
        }
        
        /*将获取的随机数验证码写入到Session变量中  --这里注意php的变量作用范围,和别的高级语言可能有些
          不同,所以要理解,自己去查手册*/
        $_SESSION['check_checks'] = $new_number;
        $num_image = imagecreate($image_width,$image_height);  //创建一个画布
        imagecolorallocate($num_image,255,255,255);  //设置画布的颜色
        for($i=0; $i<strlen($_SESSION['check_checks']); $i++)  //循环读取session变量中的验证码
        {
            $font = mt_rand(3,5); //设置随机的字体大小
            $x = mt_rand(1,8)+$image_width*$i/4;  //设置随机字符所在的位置的X坐标
            $y = mt_rand(1,$image_height/4);  //设置随机字符所在的位置的Y坐标
            //设置字符的颜色
            $color = imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
            //水平画出一行字符串 --输出字符,此函数自行查阅php手册。要GD2库支持
            imagestring($num_image,$font,$x,$y,$_SESSION['check_checks'][$i],$color);
        }
        imagepng($num_image); //生成png格式的图像
        imagedestroy($num_image);  //结束图像,释放资源
    ?>
    View Code

    PHP页面验证文件,判断是否和生成的验证码相同 (login.php):

    <?php
        header("Content-type:text/html;charset=utf-8");
        session_start();  //初始化session
        if($_POST['checks'] != "")  //判断是否请求过来的为空
        {
            //如果不为空进行一个正则的替换,替换掉所有的空格 preg_replace()函数中用''而不是""来表示
            $checks = preg_replace('/[\s| ]/','',$_POST['checks']);
            echo "<script type='text/javascript'>
                    prompt('这是您输入的验证码:','$checks');
                  </script>";
            if($checks == "")
            {
                echo "<script type='text/javascript'>
                alert('验证码不能为空');window.location.href='index.php';
                </script>";    
            }
            
            //如果用户输入验证码的值与随机生成的验证码的值相等,则弹出登录成功提示
            if($checks == $_SESSION['check_checks'])
            {
                echo "<script type='text/javascript'>
                alert('用户登录成功');window.location.href='index.php';
                </script>";    
            }
            else
            {
                echo "<script type='text/javascript'>
                alert('您输入的验证码不正确!');window.location.href='index.php';
                </script>";    
            }
        }
        else
        {
            echo "<script type='text/javascript'>
                alert('您没有输入验证码!');window.location.href='index.php';
                </script>";    
        }
    ?>
    View Code

    页面呈现登陆文件 (index.html或者index.php):

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
        //封装javascript的trim去掉字符串空格功能。
        function trim(strToTrim)
        {
            //用正则来返回替换后的字符。--功能,去掉所有的空格!包括换行符等。 关于正则知识,请自行查阅资料
            return strToTrim.replace(/(\s+)/g,"");
        }
        
        function yz()
        {
            //先用js检测是否已经输入验证码,减少服务器压力,当然服务器那边也是要验证的
            if(trim(form1.checks.value) == "")
            {
                alert("对不起!你没有输入验证码!");
                return false; //返回flase不提交
            }
            return true;
        }
    </script>
    </head>
    
    <body>
        <div id="main">
            <form action="login.php" method="post" name="form1">
                <label>验证码:</label>
                <input type="text" name="checks" />
                <img src="checks.php" title="验证码" alt="验证码显示错误" />
                <br />
                <input type="submit" value="提交验证" onclick="return yz();" />
            </form>
        </div>
    </body>
    </html>
    View Code
  • 相关阅读:
    一次“半面试”——汉得
    检查随机序列重复[Java]
    设计模式 工厂方法(Factory Method Pattern)转载
    sprintf用法详解
    MFC ComboBox的使用
    Visual Studio 2008 中程序路径配置 .
    Visual Studio 2008 、 Windows 环境变量介绍 和 如何在文件中引用环境变量 .
    MFC总结之CListCtrl用法及技巧(二) .
    C/C++中Sqlite使用简介
    getenv、setenv函数 获取和设置系统环境变量
  • 原文地址:https://www.cnblogs.com/clouds008/p/3131970.html
Copyright © 2020-2023  润新知