生成随机数验证图片的方法

<?php
session_start();
//产生一个随机的字符串验证码
$checkcode="";
for ($i=0;$i<4;$i++){
 $checkcode.=dechex(rand(0,15)); //string dechex ( int $number ) 返回一字符串,包含有给定 number 参数的十六进制表示
}
//将随机验证码保存到session中
$_SESSION[‘myCheckCode‘]=$checkcode;
//创建图片,并把上面产生的随机验证码画上去
$img=imagecreatetruecolor(100, 20);
//背景默认是黑色,可以自己设定背景颜色
$bgcolor=imagecolorallocate($img, 0, 0, 0);
//imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
imagefill($img, 0, 0, $bgcolor);
//创建新的颜色 imagecolorallocate — 为一幅图像分配颜色
//imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。
//red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。
//imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色
$white=imagecolorallocate($img, 255, 255, 255);
$blue=imagecolorallocate($img, 0, 0, 255);
$red=imagecolorallocate($img, 255, 0, 0);
$green=imagecolorallocate($img, 255, 0, 0);
//画出干扰线段
for($i=0;$i<10;$i++){
 //bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
 //imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
 imageline($img, rand(0, 100), rand(0, 20), rand(0, 100), rand(0, 20), imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255)));
}
//画出噪点
//for(){}
//把上面产生的四个随机值,字符串画上去
//bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
//imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。
//如果 font 是 1,2,3,4 或 5,则使用内置字体。
imagestring($img, rand(2, 5), rand(2, 60), rand(2, 5), $checkcode, $white);
header("content-type:image/png");
//imagepng() 将 GD 图像流(image)以 PNG 格式输出到标准输出(通常为浏览器),或者如果用 filename 给出了文件名则将其输出到该文件。
imagepng($img);

?>

随机数点击图片刷新

<img src="code.php" id="code" onclick="javascript:this.src=‘code.php?tm=‘+Math.random()" />
效果如下图,每次刷新都会随机生成一个:

点击文字刷新

<img src="code.php" id="code" onclick="javascript:this.src=‘code.php?tm=‘+Math.random()" />

第二次刷新:

在页面显示:

<div>验证码:<input type="text"><img src="上边的代码地址" onclick="javascript:this.src=‘上边的代码地址?tm=‘+Math.random()"></div>

点击图片可以进行切换

时间: 2024-08-26 16:05:35

生成随机数验证图片的方法的相关文章

生成随机数验证图片

1 <?php 2 session_start(); 3 //产生一个随机的字符串验证码 4 $checkcode=""; 5 for ($i=0;$i<4;$i++){ 6 $checkcode.=dechex(rand(0,15)); //string dechex ( int $number ) 返回一字符串,包含有给定 number 参数的十六进制表示 7 } 8 //将随机验证码保存到session中 9 $_SESSION['myCheckCode']=$che

PHP生成随机数的两种方法

这里整理了php生成随机数的二种方法,入门级的php随机数生成代码.: 第一种方法,使用系统自带的函数: srand((double)microtime()*1000000); //随机产生0-99之间的整数 $randval=rand(0,99999999); echo $randval,''; 第二种方法,不只是生成只有数字的随机字符串,更包括了各种特殊字符: function randomkeys($length){ $output=''; for($a=0;$a<$length; $a+

php生成随机数的三种方法

php生成随机数的三种方法 如何用php生成1-10之间的不重复随机数? 例1,使用shuffle函数生成随机数. <?php$arr=range(1,10);shuffle($arr);foreach($arr as $values){  echo $values." ";}?> 例2,使用array_unique函数生成随机数. <?php$arr=array();while(count($arr)<10){  $arr[]=rand(1,10);  $ar

生成字母验证图片(python)

生成字母验证图片(python) by 伍雪颖 from PILimport Image, ImageDraw, ImageFont import random def generate_authenticode(): letters = random.sample('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',4) width = 100 height = 40 im = Image.new("RGB"

PHP生成随机数的几种方法

第一种方法用mt_rand() function GetRandStr($length){ $str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $len=strlen($str)-1; $randstr=''; for($i=0;$i<$length;$i++){ $num=mt_rand(0,$len); $randstr .= $str[$num]; } return $randstr; } $numb

【JavaSE基础】生成随机数的三种方法

方法一: (数据类型)(最小值+Math.random()*(最大值-最小值+1)) 举例:  (int)(1+Math.random()*(10-1+1)) 生成1-10的随机数. 方法二: (数据类型)最小值+Math.random()*最大值 举例:  (int)(1+Math.random()*10) 生成1-10的随机数. 方法三:  通过java.util包中的Random类的nextInt方法来得到1-10的int随机数  Random ra = new Random();  ra

C#生成随机数的三种方法

随机数的定义为:产生的所有数字毫无关系. 在实际应用中很多地方会用到随机数,比如需要生成唯一的订单号. 在C#中获取随机数有三种方法: 一.Random 类 Random类默认的无参构造函数可以根据当前系统时钟为种子,进行一系列算法得出要求范围内的伪随机数. Random rd = new Random(); int i = rd.Next(); 这种随机数可以达到一些要求较低的目标,但是如果在高并发的情况下,Random类所取到的系统时钟种子接近甚至完全一样,就很有可能出现重复,这里用循环来举

java生成随机验证图片的实现

package com.fxr.生成随机图片; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; public class M

[ Python入门教程 ] Python生成随机数模块(random)使用方法

1.生成指定范围内的随机整数 >>> random.randint(0,100) 28 >>> random.randint(0,100) 36 >>> random.randint(0,100) 71 2.指定序列中随机选1个元素 >>> random.choice(range(1,100)) 10 >>> random.choice(range(1,100)) 36 >>> random.cho