
PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的,并将验证码保存在Session中。PHP生成验证码的大致流程有:
1、产生一张png的;
2、为设置背景色;
3、设置字体颜色和样式;
4、产生4位数的随机的验证码;
5、把产生的每个字符调整旋转角度和位置画到png上;
6、加入噪点和干扰线防止注册机器分析原来恶意破解验证码;
7、输出;
8、释放所占内存。
session_start();getCode(4,60,20);
function getCode($num,$w,$h) {
$code = "";
for ($i = 0; $i < $num; $i++) {
$code = rand(0, 9);
}
//4位验证码也可以用rand(1000,9999)直接生成
//将生成的验证码写入session,备验证时用
$_SESSION["helloweba_num"] = $code;
//创建,定义颜色值
header("Content-type: image/PNG");
$im = imagecreate($w, $h);
$black = imagecolorallocate($im, 0, 0, 0);
$gray = imagecolorallocate($im, 200, 200, 200);
$bgcolor = imagecolorallocate($im, 255, 255, 255);
//填充背景
imagefill($im, 0, 0, $gray);
//画边框
imagerectangle($im, 0, 0, $w-1, $h-1, $black);
//随机绘制两条虚线,起干扰作用
$style = array ($black,$black,$black,$black,$black,
$gray,$gray,$gray,$gray,$gray
);
imagesetstyle($im, $style);
$y1 = rand(0, $h);
$y2 = rand(0, $h);
$y3 = rand(0, $h);
$y4 = rand(0, $h);
imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED);
imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED);
//在画布上随机生成大量黑点,起干扰作用;
for ($i = 0; $i < 80; $i++) {
imagesetpixel($im, rand(0, $w), rand(0, $h), $black);
}
//将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
$strx = rand(3, 8);
for ($i = 0; $i < $num; $i++) {
$strpos = rand(1, 6);
imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black);
$strx += rand(8, 12);
}
imagepng($im);//输出
imagedestroy($im);//释放所占内存
}
问题一:jsp中显示验证码的代码怎么写? import javaawtColor;import javaawtFont;import javaawtGraphics;import javaawtimageBufferedImage;import javaioIOException;import javaioOutputStream;import javautilRandom;import javaximageioImageIO;/生成验证码/public class MakeCertPic { 验证码中可以出现的字符集,可以根据需要修改 private char mapTable[]={ 'a','b','c','d','e','f', 'g','h','i','j','k','l', 'm','n','o','p','q','r', 's','t','u','v','w','x', 'y','z','0','1','2','3', '4','5','6','7','8','9' };/ 功能:生成彩色验证码 参数wedth为生成的宽度,参数height为生成的高度,参数os为页面的输出流/ public String getCertPic(int width,int height,OutputStream os){ if(width>
问题二:验证码的html怎么写 JS生成验证码code {background-image: url(codejpg);font-family: Arial;font-style: italic;color: Red;border: 0;padding: 2px 3px;letter-spacing: 3px;font-weight: bolder;}unchanged {border: 0;} var code; 在全局 定义验证码 function createCode() { code = ; var codeLength = 6;验证码的长度 var checkCode = documentgetElementById(checkCode); var selectChar = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');所有候选组成验证码的字符,当然也可以用中文的 for (var i = 0; i >
问题三:获取到了手机验证码的代码怎么写 按照验证码写上去
问题四:php 怎么获取验证码的值,接着怎么写 验证码一般都保存在session中,打印一下session的值,就能看到验证码对应的字段、值;
dump($_SESSION);
问题五:如何用html编写产生验证码 html是无法写出来的,需要一种动态脚本来写例如jsp、php、asp、aspx、c#都可以实现,不过服务器需要有解析脚本的功能才可以做动态站点,不知道你网站是用什么语言
麻烦采纳,谢谢!
问题六:验证码怎么写 先建立一个ValidateCodeaspx网页;然后在你的注册页面下面:输入这段代码 验证码: 这段代码是为了在注册页面显示四位数的 验证码!然后在验证码类中输入如下代码:using System; using SystemData; using SystemConfiguration; using System。
问题七:java 登录验证码代码怎么写 原理: 1随机生成4个数字 用到了Random类 2对这4个数字设置字体格式 用 setFont方法 3改变字体颜色用setColor 然后随机生成颜色 代码如下 package s1; import javaawtColor; import javaawtFont; import javaawtGraphics; import javaa
问题八:ios 登录界面注册和获取验证码怎么做 可以参考下面的例子:
##新建一个项目
现在xcode新建的项目都是自带故事板的, *** 作不是很方便,我们来把它改成说写代码
打开AppDelegateh文件,添加以下代码
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions { selfwindow=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; selfwindowrootViewController=[[ViewController alloc] init]; [selfwindow makeKeyAndVisible]; return YES; }
到此就完成了手写代码的第一步。
添加输入框和按钮
在ViewControllerh中添加以下代码
#import ViewControllerh
@interface ViewController ()
@property (nonatomic,strong) UITextField account;
@property (nonatomic,strong) UITextField password;
@property (nonatomic,strong) UIButton loginButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[selfview setBackgroundColor:[UIColor colorWithRed:51/2550 green:204/2550 blue:255/2550 alpha:1]];
_account=[[UITextField alloc] initWithFrame:CGRectMake(20, 200, selfviewframesizewidth-40, 50)];
_accountbackgroundColor=[UIColor whiteColor];
_accountplaceholder=[NSString stringWithFormat:@Email];
[selfview addSubview:_account];
_password=[[UITextField alloc] initWithFrame:CGRectMake(20, 260, selfviewframesizewidth-40, 50)];
_passwordbackgroundColor=[UIColor whiteColor];
_passwordplaceholder=[NSString stringWithFormat:@Password];
[selfview addSubview:_password];
_loginButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[_loginButton setFrame:CGRectMake>>
问题九:html获取验证码这个按钮的代码 直接插入一个input就行了,这个input没加什么样式,最多就是加了一个宽和高的定义吧。
刚试验了一下,宽和高都没加
问题十:网页中验证码代码怎么写啊 我举个的验证码产生的代码,你可以看看: 新建yanzhengma1aspxcs文件,写入一下代码: using System; using SystemCollections; using SystemConfiguration; using SystemData; using SystemLinq; using SystemWeb; using SystemWebSecurity; using SystemWebUI; using SystemWebUIHtmlControls; using SystemWebUIWebControls; using SystemWebUIWebControlsWebParts; using SystemXmlLinq; using SystemDrawing; public partial class yanzhengma : SystemWebUIPage { private SystemIOMemoryStream ms = newSystemIOMemoryStream(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { yanzhengmayzm = new yanzhengma(); Session[code1] = yzmCreateImage(5, ValidTypeNumeric); } } / / 验证码的类型 / public enum ValidType { / / 只有数字 / Numeric, / / 数字和英文字符 / NumericAndEnglishChar, / / 中文字符 / ChineseChar } / / 生成一个随机文字,保存在 Session[code1] / / 中字的个数 / 生成的文字 public string CreateImage(int count, ValidType type) { string ValidCode = GenCode(count,type); switch (type)>>
最后两句:
$name=$_SESSION[ 'str'];
echo $name;
去掉,因为这个php实际上表现为一个,你echo是不会有值的
最后填一句 $_SESSION[ 'str'] = $str;
把这个文件保存为checkimgphp
然后,新建一个同级目录下的php文件
aphp 如下:
<php
session_start();
>
<img src="checkimgphp"/>
验证码是:
<
echo $_SESSION["str"];
>
存到$_SESSION或数据库都可以的,如$_SESSION['code'] = "xxxxxx";
设置时间也可以用$_SESSION的,保存发送验证码的时间,然后用提交的时候再获取当前时间判断是否是指定时间内(如你所说的5分钟)就可以了,如果超过了指定时间提示过期了,否则。。你先怎样就怎样了。。$_SESSION['time'] = time();当提交的时候就判断if(time()-intval($_SESSION['time'])>(560)){echo "提示过期了";}else{echo "YOUR CODE HERE";}
基本思路:
首先获取一个cookies值,再带着这个cookies去获取验证码,你再带着验证码值和登录数据去模拟post登录。下面是一个模拟获取验证码的。
这里忽略获取cookies的过程。注意文件为UTF-8无BOM格式
phpheader('Content-Type:image/png');
$url = ">
以上就是关于如何用PHP生成验证码全部的内容,包括:如何用PHP生成验证码、获取验证码代码怎么写、下面段php代码怎么得到验证码的值啊(在页面里面直接输出就可以)。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)