验证码一共有三个文件check.html,check_code.php,ValidatorCode.php
首先是check.html
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Language" content="text/html; charset=gb2312">
<title>图形验证码</title>
</head>
<body>
<form action="check_code.php" method="POST">
验证码<input type="text" name="sub_code" size="16" class="input" />
<script language="javascript">
//生成随机数
var num1=Math.round(Math.random()*100000000);
//截取随机数的前四个字符
var num=num1.toString().substr(0,4);
//将截取值传递到图像处理页中
document.write("<img name=codeimg src='ValidatorCode.phpp?code="+num+"'>");
//定义方法,重新生成验证码
function reCode(){
//生成随机数
var num1=Math.round(Math.random()*100000000);
//截取随机数的前四个字符
var num=num1.toString().substr(0,4);
//将截取到的值传递到验证码生成程序中
document.codeimg.src="ValidatorCode.php?code="+num;
}
</script>
<a href="javascript:reCode()" class="a1">看不清,重新点击</a>
<p align="center"><input type="submit" value="提交" name="B1">
</form>
</body>
</html>
ValidatorCode.php
<?php
session_start();
header('content-type:image/jpeg'); //定义JPEG格式图像
$im = imagecreate(75,25); //定义画布
//填充画布
$backcolor=imagecolorallocate($im,200,200,200);
imagefill($im,0,0,$backcolor);
$validatorCode = $_GET['code']; //获取提交的随机数
//将生成的验证码写入session
$_SESSION["validatorCode"]=$validatorCode;
//设置验证码字体的颜色
$color1= imagecolorallocate($im,0,rand(0,255),rand(0,255));
$color2= imagecolorallocate($im,rand(0,255),0,rand(0,255));
$color3= imagecolorallocate($im,rand(0,255),rand(0,255),0);
//使用GD2函数imagettftext()分别绘制四个字符
imagettftext($im,12,0,1,18,$color1,'simhei.ttf',substr($validatorCode,0,1));
imagettftext($im,12,0,14,18,$color2,'simhei.ttf',substr($validatorCode,1,1));
imagettftext($im,12,0,29,18,$color3,'simhei.ttf',substr($validatorCode,2,1));
imagettftext($im,12,0,45,18,$color1,'simhei.ttf',substr($validatorCode,3,1));
imagejpeg($im); //生成JPEG图像
imagedestroy($im); //销毁图像
?>
check_code.php
<?php
session_start();
header("Content-Type:text/html;charset=gb2312");
$code=$_POST["sub_code"];
if($code==$_SESSION["validatorCode"])
echo "验证码一致";
else
echo "验证码不一致";
?>
PDO连接数据库
一共三个文件,login.html登录,pdo_connect.php连接数据库,check_login.php验证登录
login.html
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>登陆页面</title>
</head>
<body>
<div align="center">
<form action="check_login.php" method=post>
<p align="center"><font size="5">欢迎登陆</font><br/>
用户名<input type="text" name="login_name" size="30"><br/>
密码<input type="password" name="login_password" size="30"><br/>
<input type="submit" value="提交" name="B1">
<input type="reset" value="重置" name="B2" />
</form>
</div>
</body>
</html>
pdo_connect.php
<?php
$db_name="test";
$user="root";
$password="root";
$host="localhost";
$dsn="mysql:host=$host;dbname=$db_name";//数据库名称
try{
$pdo=new PDO($dsn,$user,$password); //实例化对象
$pdo->query("set names gb2312");
}catch(Exception $e)
{
echo $e->getMessage();
}
?>
check_login.php
<?php
include_once("pdo_connect.php"); //包含pdo_connect.php文件
header("Content-Type:text/html;charset=gb2312");
$user_name=$_POST["login_name"];
$user_password=$_POST["login_password"];
if(empty($user_name) || empty($user_password))
{
echo "用户名和密码不得为空";
}
else
{
$sql="select * from user where user=:user_name and password=:user_password";
try{
$result=$pdo->prepare($sql);
$result->execute(array(':user_name'=>$user_name,':user_password'=>$user_password));
if($r=$result->fetch(PDO::FETCH_ASSOC))
{
echo "用户登陆成功";
}
else{
echo "用户名或密码不正确!登陆失败";
}
}catch(Exception $e)
{
echo $e->getMessage();
}
}
?>
先总结到自己的博客上来。
也算是今天干的事吧。