PHP简单表单登录操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/garvisjack/article/details/54836385

以下是在环境wamp下执行的,php5.4.3下的操作练习。重点是php部分的代码可以直接拿来用,样式和js比较多,未引用
login.php部分

<?php
    //生成四位的随机验证码
    $codeData ="abcdqwetyuipffhbcnvmklABCDHFUNCYEIJNDF0123456789";
    $postcode = "";
        for($i=0;$i<4;$i++){
            $postcode .= '<span class="code" style="color:rgb('.mt_rand(0,255).','.mt_rand(0,255).','.mt_rand(0,255).')">';
            $postcode.= $codeData{mt_rand(0,strlen($codeData)-1)}."</span>";
        }
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="renderer" content="webkit">
    <title>登录</title>  
    <link rel="stylesheet" href="css/pintuer.css">
    <link rel="stylesheet" href="css/admin.css">
    <script src="js/jquery.js"></script>
    <script src="js/pintuer.js"></script> 
    <style type="text/css">
    .code{
        float:left;
        height:43px;
        display:block;
        font-size: 20px;
        font-weight: bold;
        line-height: 43px;
        cursor:pointer;
    }
    </style> 
</head>
<body>
<div class="bg"></div>
<div class="container">
    <div class="line bouncein">
        <div class="xs6 xm4 xs3-move xm4-move">
            <div style="height:150px;"></div>
            <div class="media media-y margin-big-bottom">           
            </div>         
            <form action="./php/login.php" method="post">
            <div class="panel loginbox">
                <div class="text-center margin-big padding-big-top"><h1>后台管理系统</h1></div>
                <div class="panel-body" style="padding:30px; padding-bottom:10px; padding-top:10px;">
                    <div class="form-group">
                        <div class="field field-icon-right">
                            <input type="hidden"/>
                            <input type="text" class="input input-big" name="username" placeholder="登录账号" data-validate="required:请填写账号" />
                            <span class="icon icon-user margin-small"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="field field-icon-right">
                            <input type="password" class="input input-big" name="password" placeholder="登录密码" data-validate="required:请填写密码" />
                            <span class="icon icon-key margin-small"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="field">
                            <input type="hidden" name="postcode" value="<?php echo strip_tags($postcode);?>">
                            <input type="text" class="input input-big" name="code" placeholder="填写右侧的验证码" data-validate="required:请填写右侧的验证码" style="float: left;width: 270px;"/>
                            <?php  echo $postcode; ?>
                        </div>
                    </div>
                    <a href="submit.html" target="_blank" style="float:right;margin-top:5px;">没有帐号?注册新用户</a>
                </div>
                <div style="margin-top:6px;padding:30px;"><input type="submit" class="button button-block bg-main text-big input-big" value="登录"></div>
            </div>
            </form>          
        </div>
    </div>
</div>

</body>
</html>

操作部分login.php

<?php
    header("content-type:text/html;charset=utf-8");
    require_once('../../connect.php');
    if(!empty($_POST)){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $code = trim($_POST['code']);
    $postcode = $_POST['postcode'];
    $selectData = "select username,password from user where username='$username' and password='$password'";
    $selectQuery = mysql_query($selectData);
        if($selectQuery&&mysql_num_rows($selectQuery)){
            if(strcasecmp($code, $postcode)==0){
            $row = mysql_fetch_assoc($selectQuery);
            echo "<script>alert('登录成功!点击跳转');location.href='../index.html';</script>";
            }
        }
        else{
            echo "<script>alert('帐号或密码不确!');location.href='../login.php';</script>";
        }

    }

?>

没有更多推荐了,返回首页