php 使用GD库生成验证码
GD库是PHP进行图象操作一个很强大的库。
先在php.ini里增加一行引用:extension=php_gd2.dll
重启apache。做一个测试页 var_dump(gd_info());输出数据表明GD库引用成功。
表单auth.html
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>验证码</title>
</head>
<body>
<h1>请输入验证码</h1>
<form action="check_auth.php" method="post">
<input name="auth" type="text">
<img src="auth.php" border="0" />
<input type="submit" value="提交">
</form>
</body>
</html>
生成验证码 auth.php
<?php
session_start();
header("Content-type:image/png");
$img_width=100;
$img_height=20;
srand(microtime()*100000);
for($i=0;$i<4;$i++)
{
$new_number.=dechex(rand(0,15));
}
$_SESSION[check_auth]=$new_number;
$new_number=imageCreate($img_width,$img_height);//创建图象
ImageColorAllocate($new_number,255,255,255); //设置背景色为白色
for($i=0;$i<strlen($_SESSION[check_auth]);$i++)
{
$font=mt_rand(3,5);
$x=mt_rand(1,8) + $img_width*$i/4;
$y=mt_rand(1,$img_height/4);
$color=imageColorAllocate($new_number,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));//设置字符颜色
imageString($new_number,$font,$x,$y,$_SESSION[check_auth][$i],$color);//输出字符
}
ImagePng($new_number);
&nb
相关文档:
安装以及配置
1. 安装Apache-2.2.4(Apache_Dir=D:\myspace\Apache-2.2.4)
2. 解压安装PHP-5.2.11(PHP_Dir=D:\myspace\PHP-5.2.11)
3. 找到PHP-5.2.11下的php.ini-dist,改名为php.ini
4. &nb ......
<?php
//作者:梁文平 http://www.tyasp.net
session_start();
if($_SESSION["username"]!="admin")
{
echo("<mce:script type="text/javascript"><!--
alert("操作超时!请重新登陆...");window.location.href="../index.php";
// --></mce:script>");
//header("refresh:0;url=../"); ......
<?php
// 将数组转换成Json格式,中文需要进行URL编码处理
function Array2Json($array) {
arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
$json = urldecode($json);
// ext需要不带引号的bool类型
&n ......
<?php
$start = ip2long('192.168.1.1');
$start = sprintf("%u", $start) ;
$end = ip2long('192.168.1.50');
$end = sprintf("%u", $end) ;
for ($start; $start<$end; $start++){
echo long2ip($start)."<br>";
}
?> ......