+ -
当前位置:首页 → 问答吧 → 习作用户登录form带图片验证

习作用户登录form带图片验证

时间:2009-03-21

来源:互联网

刚学php,最幸运的是很快就找到了phpchina这块宝地。
test.rar (116.81 KB)
代码文件
下载次数: 183
2009-3-21 00:02

---------------------------------------------------------------
为方便大家看,还是把代码帖出来。

生成验证图片
[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>
                密&nbsp;&nbsp;&nbsp;&nbsp;码:
            </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="登&nbsp;&nbsp;陆" />
            </td>
            <td>
                <input type="reset" id="reset" value="重&nbsp;&nbsp;置" />
            </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 &copy; 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

热门下载

更多