php 验证码

php 验证码

来源: 慕课网教程

产生一张图片

<?php
$image = imagecreatetruecolor(300,200);
$white = imagecolorallocate($image, 255,255,255);
$black = imagecolorallocate($image, 0, 0, 0);

imagefill($image, 0, 0, $white);
header(‘content-type: image/png‘);
imagepng($image);
imagedestroy($image);
?>
<?php
$filename = dirname(__FILE__).DIRECTORY_SEPARATOR.‘pic0.jpg‘;
$contents = file_get_contents($filename);
header(‘content-type: image/jpg‘);
echo $contents;
?>

添加数字

<?php
$image = imagecreatetruecolor(300,200);
$white = imagecolorallocate($image, 255,255,255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);

// 随机数字
for ($i = 0; $i < 4; $i++) {
    $fontsize = 6;
    $fontcontent = rand(0, 9);
    $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
    $x = $i * 100/4 + rand(5, 10);
    $y = rand(5, 10);
    imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
header(‘content-type: image/png‘);
imagepng($image);
imagedestroy($image);
?>

干扰点和干扰线

for ($i = 0; $i < 150; $i++) {
    $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
    imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor);
}

for ($i = 0; $i < 4; $i++) {
    $linecolor = imagecolorallocate($image, rand(150, 200), rand(150, 200), rand(150, 200));
    imageline($image, rand(1, 99), rand(1, 20), rand(1, 99), rand(1, 20), $linecolor);
}

实现字母和数字的验证码

$data = ‘3456789abcdefghjkmnpqrstuvwxy‘;   // 去掉 0 和 o, 1 和 l
$len = strlen($data);
for ($i = 0; $i < 4; $i++) {
    $fontsize = 8;
    $fontcontent = $data[rand(0, $len)];
    $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
    $x = $i * 100/4 + rand(5, 10);
    $y = rand(5, 10);
    imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}

验证登陆

verify.php

<?php
session_start();
$image = imagecreatetruecolor(100,30);
$white = imagecolorallocate($image, 255,255,255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);

$captch_code = ‘‘;

$data = ‘3456789abcdefghjkmnpqrstuvwxy‘;
$len = strlen($data);
for ($i = 0; $i < 4; $i++) {
    $fontsize = 8;
    $fontcontent = $data[rand(0, $len)];
    $captch_code .= $fontcontent;
    $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
    $x = $i * 100/4 + rand(5, 10);
    $y = rand(5, 10);
    imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}

$_SESSION[‘authcode‘] = $captch_code;

for ($i = 0; $i < 150; $i++) {
    $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
    imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor);
}

for ($i = 0; $i < 4; $i++) {
    $linecolor = imagecolorallocate($image, rand(150, 200), rand(150, 200), rand(150, 200));
    imageline($image, rand(1, 99), rand(1, 20), rand(1, 99), rand(1, 20), $linecolor);
}

header(‘content-type: image/png‘);
imagepng($image);
imagedestroy($image);
?>

form.php

<?php
if (isset($_REQUEST[‘authcode‘])) {
    session_start();

    if (strtolower($_REQUEST[‘authcode‘]) == $_SESSION[‘authcode‘]) {
        echo ‘<font color="#00C"> succees </font>‘;
    } else {
        echo ‘<font color="#C00"> fail </font>‘;
    }
    exit();
}
?>

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <title>test</title>
  </head>
  <body>
    <form method="post" action="<?php echo $_SERVER[PHP_SELF]; ?>">
      <p>验证码图片: <img border="1" src="./verify.php?r=<?php echo rand(); ?>" width="120px"></p>
      <p><input type="text" size="10" name="authcode" value=""></p>
      <p><input type="submit" value="submit"> </p>
    </form>
  </body>
</html>

实现刷新

<?php
if (isset($_REQUEST[‘authcode‘])) {
    session_start();

    if ($_REQUEST[‘authcode‘] == $_SESSION[‘authcode‘]) {
        echo ‘<font color="#00C"> succees </font>‘;
    } else {
        echo ‘<font color="#C00"> fail </font>‘;
    }
    exit();
}
?>

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <title>test</title>
  </head>
  <body>
    <form method="post" action="<?php echo $_SERVER[PHP_SELF]; ?>">
      <p>
        验证码图片: <img id="captcha_img" border="1" src="./verify.php?r=<?php echo rand(); ?>" width="120px">
        <a href="javascript:void(0)" onclick="document.getElementById(‘captcha_img‘).src=‘./verify.php?r=‘+Math.random()">换一个?</a>
      </p>
      <p><input type="text" size="10" name="authcode" value=""></p>
      <p><input type="submit" value="submit"> </p>
    </form>
  </body>
</html>

图片验证码

form_img.php

<?php
session_start();

$table = array(
    ‘pic0‘ => ‘dog‘,
    ‘pic1‘ => ‘cat‘,
    ‘pic2‘ => ‘fish‘,
    ‘pic3‘ => ‘bird‘,
);

$index = rand(0, 3);

$value = $table[‘pic‘.$index];
$_SESSION[‘authcode‘] = $value;

$filename = dirname(__FILE__).DIRECTORY_SEPARATOR.‘pic‘.$index.‘.jpg‘;
$contents = file_get_contents($filename);
header(‘content-type: image/jpg‘);
echo $contents;
?>

<?php
if (isset($_REQUEST[‘authcode‘])) {
    session_start();
    if ($_REQUEST[‘authcode‘] == $_SESSION[‘authcode‘]) {
        echo ‘<font color= "#f00";>succeed</font>‘;
    } else {
        echo ‘<font color= "#00f";>fail</font>‘;
    }
    exit();
}
?>

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <title>test</title>
  </head>
  <body>
    <form methos="post" action="<?php echo $_SERVER[PHP_SELF]; ?>">
      <p>图片: <img id="captcha_img" src="./verify_img.php?r=<?php echo rand(); ?>" width="200" height="100">
        <a href="javascript:void(0)" onclick="document.getElementById(‘captcha_img‘).src=‘./verify_img.php?r=‘+Math.random()">换一个</a>
      </p>
      <p><input type="text" name="authcode" size="10"></p>
      <input type="submit" value="submit">
    </form>
  </body>
</html>

verify_img.php

<?php
session_start();

$table = array(
    ‘pic0‘ => ‘dog‘,
    ‘pic1‘ => ‘cat‘,
    ‘pic2‘ => ‘fish‘,
    ‘pic3‘ => ‘bird‘,
);

$index = rand(0, 3);

$value = $table[‘pic‘.$index];
$_SESSION[‘authcode‘] = $value;
$filename = dirname(__FILE__).DIRECTORY_SEPARATOR.‘pic‘.$index.‘.jpg‘;
$contents = file_get_contents($filename);
header(‘content-type: image/jpg‘);
echo $contents;
?>
时间: 2024-08-08 21:56:24

php 验证码的相关文章

php 扭曲验证码

1 <?php 2 3 class image{ 4 public static function code(){ 5 6 $str='abcdefghijklmnopqrstuvwxyz0123456789'; 7 $code = substr(str_shuffle($str),0,5); 8 9 // 2块画布 10 $src = imagecreatetruecolor(60, 25); 11 $dst = imagecreatetruecolor(60, 25); 12 13 // 灰

发送短信验证码按钮 定时器

static int i = 29; @property(nonatomic,strong) NSTimer *timmer;//定时器 - (void)sendNumber{ NSLog(@"发送验证码"); self.getTelephoneCodeBtn.enabled = NO; [self.getTelephoneCodeBtn setTitle:@"已发送" forState:UIControlStateDisabled]; [self.getTelep

ThinkPHP 3.2.3 加减乘法验证码类

ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Verify/ 可以在 Verify.class.php 文件内进行修改,也可以单独写一个类继承自带的验证码类.如果单独写一个继承的类,可以重用父类的属性和方法,但是要注意的是父类中有一些属性和方法是私有(private)的,可以修改这些私有的属性和方法为保护(protected)的,如果不希望修改框架自

制作验证码时 图片无法刷新

1在网页上制作上图中的验证码时,点击图片不能刷新依旧是原图,找了好久终于发现是js语句中的错误,如下: 如果不加后面的new Date().getTime(),每次请求/AuthCode/getAuthCode都是一样的所以图片不会变化,加上之后每次请求会变化,就会重新加载图片了1 function reload(){                    document.getElementById("bt").src="/AuthCode/getAuthCode?&qu

验证码

Random yan=new Random();//随机数种子,注意括号里不要填数 String a="0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";//验证码需要的所有字符 String b []=a.split("");//定义一个数组,用““把每个字符分割 System.out.print("生成的验证码为");//第一种方法 /*for(int i = 0;

网站验证码制作

asp.net验证码制作 using System; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Text; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebPa

自动化测试--登录页面验证码问题

对于web应 用来说,大部分的系统在用户登录时都要求用户输入验证码,验证码的类型的很多,有字母数字的,有汉字的,甚至还要用户输入一条算术题的答案的,对于系统来 说使用验证码可以有效果的防止采用机器猜测方法对口令的刺探,在一定程度上增加了安全性.但对于测试人员来说,不管是进行性能测试还是自动化测试都是一个 棘手的问题. 下面来谈一下处理验证码的几种方法. 去掉验证码 这是最简单的方法,对于开发人员来说,只是把验证码的相关代码注释掉即可,如果是在测试环境,这样做可省去了测试人员不少麻烦,如果自动化脚

JCaptcha生成计算式验证码

前两天买了张火车票,令人egg碎的验证已经无力吐槽,试了几次都让我开始怀疑我的IQ是不是适合再从事程序猿的这个"神圣的职业"了.今天分享的是一段比较传统而不失实用的验证,验证的意义想必大家都已知晓. 好了,语言总是苍白无力.直入真题来看代码的实现的吧. 操作环境: jboss eap 6.2 tomcat也可以,比较懒直接用的jboss.大家有兴趣也可以试试,支持的服务更多,性能更加稳定些. servlet 麻雀虽小,五脏俱全,做了小例子足以. JCaptcha jcaptcha是一个

PHP利用GD库画图和生成验证码图片

首先得确定php.ini设置有没有打开GD扩展功能,测试如下 print_r(gd_info()); 如果有打印出内容如下,则说明GD功能有打开: Array ( [GD Version] => bundled (2.0.34 compatible) [FreeType Support] => 1 [FreeType Linkage] => with freetype [T1Lib Support] => 1 [GIF Read Support] => 1 [GIF Crea

session cookie 验证码实现

using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Blog.UI { using System.Drawing; using Blog.Common; /// <summary> /// Vcode 的摘要说明 /// </summary> public class Vcode : IHttpHandler,System.Web.Session