习作用户登录form带图片验证
时间:2009-03-21
来源:互联网

---------------------------------------------------------------
为方便大家看,还是把代码帖出来。
生成验证图片
[php]
<?php
/**
* imagecode class
* @param int $width
* @param int $height
* @param string $text //random string
* @param int $lines //how many lines to draw
* @example
* <?php
* $img=new Imagecode(int $width,int $height,string $text,int $lines);
* $img->drawCode();
* $img=null;
* ?>
* */
if(!defined('IN'))
{
exit('Access Denied.');
}
class Imagecode
{
private $width;
private $height;
private $text;
private $lines;
private $fnt;
private $fs;
private $img;
private $clb;
private $clf;
function __construct($width,$height,$text,$lines,$fnt='georgia.ttf')
{
$this->width=$width;
$this->height=$height;
$this->text=$text;
$this->lines=$lines;
$this->fnt=$fnt;
$this->fs=rand(14,20);
$this->img=imagecreatetruecolor($this->width,$this->height);
$seed=array(rand(128,191),rand(128,191),rand(128,191));
$this->clb=imagecolorallocate($this->img,$seed[0],$seed[1],$seed[2]);
$this->clf=imagecolorallocate($this->img,$seed[0]+64,$seed[1]+64,$seed[2]+64);
}
protected function drawLine($lines)
{
imagesetthickness($this->img,2);
for($i=0;$i<$this->lines;$i++)
{
$x1=rand(0,$this->width);
$x2=rand(($this->width-$x1)/2,$this->width);
$y1=rand(0,$this->height);
$y2=rand(($this->height-$y1)/2,$this->height);
imageline($this->img,$x1,$y1,$x2,$y2,$this->clf);
}
}
public function drawCode()
{
header('Content-type:image/gif');
imagefill($this->img,0,0,$this->clb);
$this->drawline($this->lines);
$direction=rand(0,1)>0?1:-1;
$angle=rand(0,5)*$direction;
if(!file_exists($this->fnt))
{
exit('没找到指定字体!');
}
imagettftext($this->img,$this->fs,$angle,(20-$this->fs)*2,26,$this->clf,$this->fnt,$this->text);
imagegif($this->img);
}
function __destruct()
{
imagedestroy($this->img);
}
}
?>
[/php]
调用图片
[php]
<?php
define('IN',true);
include('imagecode.php');
$url=parse_url($_SERVER['HTTP_REFERER']);
if(($url['host']!=$_SERVER['HTTP_HOST'])||empty($url)||($url['path']==$_SERVER['SCRIPT_NAME']))
{
exit('Access Denied.');
}
$rnd=rand(1048576,15728639);
$hex=strtoupper(dechex($rnd));
$img=new Imagecode(100,40,$hex,2);
$img->drawcode();
$img=null;
?>
[/php]
登录Form
[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 id="Head1" runat="server">
<title>后台管理--用户登录</title>
<script type="text/javascript" language="javascript" src="jquery.js"></script>
<script type="text/javascript" language="javascript">
function vid(o)
{
return $.trim($(o).val());
}
</script>
<style type="text/css">
body {
font-size: 12px;
}
#user, #pwd, #vcode {
width: 140px;
}
.bk{border:1px solid black; width:370px; height:200px;margin:0 auto;background:#eeeeee/*#f0f6f9*/}
#login{margin-top:10px; margin-left:30px;}
</style>
</head>
<body>
<div class="bk">
<div style="font-size:14px; font-weight:bold; margin-left:30px; margin-top:10px;">后台管理员登录</div>
<form id="login" method="post" action="login.php">
<table border="0" cellpadding="5" cellspacing="0">
<tr>
<td>
验证码:
</td>
<td>
<input type="text" id="vcode" name="vcode" title="请输入验证码" tabindex="1" />
</td>
<td rowspan="3" valign="top" style="line-height: 180%">
<img id="code" width="100px" height="40px;" src="gif.php" alt="验证码图片"
title="验证码" style="margin-top: 5px; margin-bottom: 5px; cursor: pointer" /><br />
看不清请点击图片<br />
</td>
</tr>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" id="user" name="user" title="请输入用户名" tabindex="2" />
</td>
</tr>
<tr>
<td>
密 码:
</td>
<td>
<input type="password" id="pwd" name="pwd" title="请输入密码" tabindex="3" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" id="sub" name="sub" value="登 陆" />
</td>
<td>
<input type="reset" id="reset" value="重 置" />
</td>
</tr>
</table>
</form>
<div style="text-align:center; margin-bottom:5px; background:#000; height:20px; line-height:20px;
color:#999; font-size:13px; font-family:Arial; border:1px solid white ">Copyright © 2009
</div>
</div>
<script type="text/javascript" language="javascript">
function verify()
{
if (vid("#vcode") == "") { alert("验证码不能空!"); return false; }
if (vid("#user") == "") { alert("用户名不能空!"); return false; }
if (vid("#pwd") == "") { alert("密码不能空!"); return false; }
}
function change(el)
{
var s = el.src;
var rd = Math.random()*1000000;
rd = Math.ceil(rd);
var len =s.indexOf("?");
if(len>0)
{
el.src=s.substr(0,len+1)+rd.toString();
}
else
{
el.src=s+"?"+rd.toString();
}
}
</script>
</body>
</html>
[/php]
作者: whqida 发布时间: 2009-03-21
作者: E蜗牛 发布时间: 2009-03-21
作者: joj_79 发布时间: 2009-03-21
作者: E蜗牛 发布时间: 2009-03-21
作者: joj_79 发布时间: 2009-03-22
作者: xinxing520 发布时间: 2009-03-22
作者: sanfe 发布时间: 2009-03-23
作者: JOJ_97 发布时间: 2009-03-23
作者: kwlong2008 发布时间: 2009-03-24
作者: xiumeng 发布时间: 2009-04-12
作者: flyboyxiang 发布时间: 2009-05-07
作者: fruitsdrink 发布时间: 2009-05-07
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28