login.php代码如下:
<html>
<head>
    <meta charset="utf-8">
    <title>后台登陆</title>
    <style>
        html,body{margin: 0;padding:0;background:#26272b; }
        form{width:500px;height:350px;text-align:center;margin: 100px auto 0;background: #f1f2f9;border-radius:8px; }
        form h3{matgin:0 0 50px 0;height:50px;font:28px/56px 微软雅黑;background:#dfe0e7;
            border-bottom:1px solid #26272b;border-radius:8px 8px 0 0}
        .input1{font:16px/32px 微软雅黑;width:240px;}
        .btn1{font:18px/36px 微软雅黑;width:160px;border:0;background:#30ae3c;border-radius:4px;
            color:#fff;margin-top:30px;}
    </style>
</head>
<body>
<form action="login_check.php" method="post">
    <h3>后台登陆</h3>
    <p>登陆名:<input type="text" name="username" class="input1"/></p>
    <p>密&emsp;码:<input type="password" name="password" class="input1"/></p>    <p>
        验证码:<img id="captch_code" border="1" src="./captcha.php?r=<?php echo rand();?>" witdh="100" height="30" />
        <a href="javascript:void(0)" onclick="document.getElementById('captch_code').src='./captcha.php?r='+Math.random()">换一个</a>
    </p>
    <p>输入图片中的内容:<input type="text" name="authcode" value="" /> </p>
    <p><input type="submit" value="立即登陆" class="btn1" /></p>
</form>
</body>
</html>
captcha.php 代码如下:
<?php
include('../conn.php');      //这里要两个点是conn.php放在其上一级目录
session_start();   //开启session//实现生成数字字母验证码的功能$image=imagecreatetruecolor(100,30);         //用imagecreatetruecolor()函数生成画布$image 默认背景为黑色,大小(100,30)
$bgcolor=imagecolorallocate($image,255,255,255);    //将默认背景设为白色(255,255,255)
imagefill($image,0,0,$bgcolor);    //从坐标0,0开始,用$bgcolor 填充$image//下面是字母和数字混合的验证码
$captch_code="";     //定义一个变量,并为空
for($i=0;$i<4;$i++){
    $fontsize=6;    //设置字体大小为6
    $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));  //用函数产生随机的RGB值的字体颜色,rand中的值越小颜色越深    $data='abcdefghjklmnpqrstuvwxy23456789';    //去掉容易误会的ioz和1
    $fontcontent=substr($data,rand(0,strlen($data)),1);   //在$data字典中从第0 个开始随机截取一个
    $captch_code.=$fontcontent;    //每产生一个字符就拼接到 $captch_code    $x=($i*100/4)+rand(5,10);    //x轴的位置:100是画布宽度,4是产生4个随机数
    $y=rand(5,10);               //y轴上的位置(先于X轴确定)    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);   //水平地画一行字符串
}
$_SESSION['authcode']=$captch_code;   //将验证码内容保存到SESSION的 authcode 变量中for($i;$i<200;$i++){
    $pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(500,200));   //用函数产生随机的RGB值的干扰点
    imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);   //imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
}
for($i=0;$i<3;$i++){
    $linecolor=imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));
    imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);     //一根线有两个点组成
}header('content-type:image/png');
imagepng($image);  //imagepng — 以 PNG 格式将图像输出到浏览器或文件,,,还有 imagegif(),imagewbmp(),imagejpeg() 和 imagetypes()。
imagedestroy($image);    //销毁资源login_check.php代码如下:
<?php
include('../conn.php');      //这里要两个点是conn.php放在其上一级目录
session_start();//接收数据
$username=$_POST['username'];     //前台form表单提交
$password=$_POST['password'];     //前台form表单提交
$authcode=$_POST['authcode'];    //前台form表单提交
$authcode1=$_SESSION['authcode'];   //captcha.php 文件中生成的验证码写入到session中//验证数据有效性
if(strlen($username)<5){
    echo '用户名不能少于5个字';  exit;
}//下面是验证码代码块,但是还有问题,点击登录后没有东西,也不报错
if($authcode==$authcode1){
    //构造SQL语句,去数据库验证用户名和密码是否正确
    $sql="select * from admin where username='$username' and password='$password'";
    $rs=mysqli_query($conn,$sql);
    //mysqli_error($conn);//将验证的结果显示出来
    if($row=mysqli_fetch_assoc($rs)){
        //登陆成功,写SESSION
       // session_start();
        $_SESSION['userid']=$row['id'];
        $_SESSION['username']=$row['username'];
        $_SESSION['flag']=$row['flag'];
        //给自定义函数 alert() 传入两个值
        alert('恭喜你,登陆成功!','index.php');
    }else{
        alert('登陆失败,请检查用户名或密码是否正确','login.php');
    }
}输入用户名、密码和验证码后不能跳转到index.php主页,只是显一个里面带叉的框框小图片