欢迎来到资源库(www.zyku.net)

Python教程

当前位置:首页 > 网络编程 > Python教程 > flask框架

flask框架实现修改密码和免密登录功能

时间:2022-02-10|栏目:Python教程|点击:|我要投稿

修改密码功能

数据库部分:

#重置密码
def reset_pass(phone,password):
    conn,cursor=get_conn()
    sql="update userdata set userpass='"+password+"' where userphone='"+phone+"'"
    try:
        print("正在修改密码...")
        resetflag=cursor.execute(sql)
        conn.commit()
        close_conn(conn,cursor)
        if(resetflag==1):
            print("修改成功")
            return 1
        else:
            print("修改失败!")
            return 0
    except:
        print("系统错误...修改密码失败!")
        return 0

路由部分:

#用户修改密码
@app.route('/resetpass',methods=['GET', 'POST'])
def resetpass():
    userphone=request.values.get('userphone')
    resetpass=request.values.get('resetpass')
    print("路由获得手机号:"+userphone+"\n")
    print("路由获得新密码:" + resetpass + "\n")
    flag=sql.reset_pass(userphone,resetpass)
    if(flag==1):
        return jsonify({"data":1})
    else:
        return jsonify({"data":0})

html页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>树懒电影---重置您的密码</title>
    <style type="text/css">
        #resetform{
            margin-top: 350px;
            margin-left: 750px;
        }
    </style>
</head>
<body>
    <form method="post" id="resetform">
        <tr>
            <td><input type="text" id="userphone" value="" placeholder="输入您的手机号码"></td>
        </tr><br>
        <tr>
            <td><input type="password" id="resetpass1" value="" placeholder="输入您的新密码"></td>
        </tr><br>
        <tr>
            <td><input type="password" id="resetpass2" value="" placeholder="再次输入您的新密码"></td>
        </tr><br>
        <tr>
            <td><input type="reset" value="清空"></td>
            <td><input type="button" id="resetbtn" onclick="resetpass()" value="提交"></td>
        </tr>
    </form>
</body>
</html>
<script src="../static/js/jquery.min.js"></script>
<script type="text/javascript">
    function resetpass(){
        var userphone=document.getElementById("userphone").value
        var resetpass1=document.getElementById("resetpass1").value
        var resetpass2=document.getElementById("resetpass2").value
        var submit_flag=1
        //判空
        if((userphone.length==0)||(resetpass1.length==0)||(resetpass2.length==0)){
            submit_flag=0
            alert("请把信息填写完整!")
        }
        //判断密码一致性
        if(resetpass2!=resetpass1){
            submit_flag=0
            alert("两次填写的密码不一致")
            document.getElementById("resetpass1").focus();
        }
        //判断手机号
        if(userphone.length!=11){
            submit_flag=0
            alert("手机号码应为11位!")
            document.getElementById("userphone").focus();
        }
        var regu =  /^1[3456789]\d{9}$/
        if(!(regu.test(userphone)) ){
            submit_flag=0
            alert("手机号码格式有误!")
            document.getElementById("userphone").focus();
        }
        //判断密码格式
        if(!((resetpass1.length>=6)&&resetpass1.length<=18))
        {
            submit_flag=0
            alert("密码长度应该为6-16位!")
            document.getElementById("resetpass1").focus();
        }
            var regex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z]).{6,18}');
            part_pass=resetpass1.split(" ")
        if((!(regex.test(resetpass1))) || part_pass.length!=1)
        {
            submit_flag=0
            alert("密码为数字+英文字母 且不可以包含空格!")
            document.getElementById("resetpass1").focus();
        }

        //发起请求
            if(submit_flag==1)
        {
            $.ajax({
                url:"/resetpass",
                data:{userphone:userphone,resetpass:resetpass2},
                success: function (data) {
                    if (data.data==1)
                    {
                        alert("密码修改成功!")
                        window.open("/",'_self')
                    }
                    else
                    {
                        alert("修改密码失败!请重试")
                    }
                },
                error: function (xhr, type, errorThrown) {
                    // print("ajax请求失败!")
                }
            })
        }
        // alert(submit_flag)
    }
</script>

免密登录

html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="../static/css/login.css" rel="external nofollow" >
    <script src="../static/js/jquery-1.7.2.min.js"></script>
    <script src="../static/js/register.js"></script>
    <title>树懒电影登录</title>
    <script >
    </script>
</head>
<body>
    <div id="container">
        <div id="container-child">
            <div id="img-div"><img src="../static/img/shulan.png"></div>
            <div id="login-div">
                <div>
                    <p>登录您的树懒电影</p>
                    <form id="login-form" method="post">
                        <div>
                            <input type="text" name="userphone" id="userphone" placeholder="请输入您的账号">
                        </div>
                        <div>
                            <input type="password" name="password" id="password" placeholder="请输入您的密码">
                        </div>
                        <div>
                            <div>
                                <input type="checkbox" value=“1” class=“remeber” onclick="onClickHander(this)">
                                <label>记住密码</label>
                            </div>
                        </div>
                        <button type="button" onclick="login_()">登 录</button>
                        <div> <a href="http://127.0.0.1:5000/regis" rel="external nofollow" >注册</a> | <a href="http://127.0.0.1:5000/reset" rel="external nofollow" >忘记密码</a></div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<script>
    var cb=0
    function onClickHander(obj) {
        if(obj.checked==true){
            cb=1
            // alert(cb)
        }else{
            cb=0
            // alert(cb)
        }
    }
    function login_(){
        var userphone=document.getElementById("userphone").value
        var password=document.getElementById("password").value
        // alert(cb)
        $.ajax({
                url: "/web_login",
                data: {
                    userphone:userphone,password:password,cb:cb
                },
                success: function (data) {
                    //正常验证失败弹窗
                    if (data.data == 0)
                        alert("账号或密码错误!")
                    //验证成功,返回response
                    if (data.data != 0)
                        window.open("http://127.0.0.1:5000/show","_self")
                },
                error: function (xhr, type, errorThrown) {
                    print("登录js,验证账号密码ajax请求失败!")
                }
        })
    }
</script>

Python路由

#免密登录
@app.route('/web_login/',methods=['GET', 'POST'])
def web_login():
    userphone = request.values.get('userphone')
    password=request.values.get('password')
    cb=request.values.get('cb')
    print("是否记住密码: "+cb)            #cb的返回值类型是 str 字符串
    # print(type(cb))
    print("登录账号:"+userphone+"   "+"密码:"+password)
    res=sql.web_login(userphone,password)
    if(res==True):
        session['userphone'] = userphone
        if(cb=="1"):
            print("开始存储cookie登录账号:" + userphone + "   " + "密码:" + password)
            resp = make_response('储存cookie')
            resp.set_cookie('cookphone', userphone, max_age=3600 * 24 * 15)
            resp.set_cookie('cookpass', password, max_age=3600 * 24 * 15)
            print("登录成功且用户选择记住密码,返回response")
            return resp                   #登录成功且用户选择记住密码,返回response
        else:
            print("登录成功 返回 1 状态码")
            return jsonify({"data": 1})  # 登录成功 返回 1 状态码
    else:
        print("登录失败   返回 0 状态码")
        return jsonify({"data":0})  #登录失败   返回 0 状态码

数据库验证登录

# 用户(web)登录验证
def web_login(userphone, password):
    cursor = None
    conn = None
    res=[]
    if(userphone==None or password==None):
        return False
    conn, cursor = get_conn()
    sql = "select userphone,userpass from userdata where '"+userphone+"'=userphone and '"+password+"'=userpass "
    res=query(sql)
    conn.commit()
    if(len(res)==0):
        print("登陆失败(WEB)")
        close_conn(conn, cursor)
        return False
    else:
        close_conn(conn, cursor)
        print("登陆成功(WEB)")
        return True

(资源库 www.zyku.net)

原文链接:https://www.cnblogs.com/rainbow-1/p/14800247.html

上一篇:python 实现二维数组的索引、删除、拼接操作

栏    目:Python教程

下一篇:python使用多线程备份数据库的步骤

本文标题:flask框架实现修改密码和免密登录功能

本文地址:https://www.zyku.net/python/9714.html

关于我们 | 版权申明 | 寻求合作 |

重要申明:本站所有的文章、图片、评论等内容,均由网友发表或上传并维护或收集自网络,仅供个人学习交流使用,版权归原作者所有。

如有侵犯您的版权,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:95148658 | 邮箱:mb8#qq.com(#换成@)

苏ICP备2020066115号-1

本网站由提供CDN加速/云存储服务