php 简单的得登录
RandImage.php
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?php
session_start();
function random($len)
{
$srcstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
mt_srand();
$strs="";
for($i=0;$i<$len;$i++){
$strs.=$srcstr[mt_rand(0,35)];
}
return strtoupper($strs);
}
$str=random(4); //随机生成的字符串
$width = 50; //验证码图片的宽度
$height = 25; //验证码图片的高度
@header("Content-Type:image/png");
$_SESSION["code"] = $str;
//echo $str;
$im=imagecreate($width,$height);
//背景色
$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
//模糊点颜色
$pix=imagecolorallocate($im,187,230,247);
//字体色
$font=imagecolorallocate($im,41,163,238);
//绘模糊作用的点
mt_srand();
for($i=0;$i<1000;$i++)
{
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
}
imagestring($im, 5, 7, 5,$str, $font);
imagerectangle($im,0,0,$width-1,$height-1,$font);
imagepng($im);
imagedestroy($im);
$_SESSION["code"] = $str;
?>
login.html
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<html>
<body>
<form name="login" action="login.php" method="POST">
<input type="text" name="name"/>
<p><input type="password" name="password"/> </p>
<input type="text" name="rand"/>
<img alt="" src="RandImage.php" />
<input name="log" type="submit" value="登录">
</form>
</body>
</html>
login.php
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?php
session_start();
$name=$_POST["name"];
$passowrd=$_POST["password"];
$conn=mysqli_connect("localhost", "root", "gwazycn","guest_book",3306); //打开MySQL服务器连接
mysqli_query($conn,"set names GB2312"); //解决中文乱码问题
$exec="select * from userlog where user_name='$name' and user_pass='$passowrd' "; //sql语句
$res = mysqli_query($conn,$exec);
$rows=mysqli_num_rows($res);
echo $exec;
if($rows)
{
if($_POST["rand"]==$_SESSION["code"])
{
echo "成功";
}
else
{
echo "验证码错误";
}
}
else
{
echo "用户名或者密码错误";
}
mysqli_close($conn);
?>