千家信息网

vue加php如何实现登陆

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要讲解了"vue加php如何实现登陆",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"vue加php如何实现登陆"吧!vue加php实现登录的方
千家信息网最后更新 2026年11月02日vue加php如何实现登陆

这篇文章主要讲解了"vue加php如何实现登陆",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"vue加php如何实现登陆"吧!

vue加php实现登录的方法:1、创建登录部分的代码文件;2、创建Javascript代码;3、在vue项目中使用PHP来做用户的注册和登录功能即可。

本文操作环境:Windows7系统,PHP7.1版,Dell G3电脑。

Vue + PHP 做用户注册登录功能

对于一款应用来说,最基本的就是用户的注册和登录功能,这篇博客就总结一下在vue项目中如何使用PHP来做用户的注册和登录功能。

登录部分:
HTML
Javascript
import $ from './js/jquery.js';import './css/mobilecommon.css';import Vue from '../node_modules/vue/dist/vue.js';$(function(){    var vm = new Vue({        el:"#app",        data:{            userid:'',            usercode:'',            errorFlag:false,            errorMsg:''        },        methods:{            login(){                var thisvue = this;                if(thisvue.userid==''||thisvue.usercode=='')                {                    thisvue.errorMsg = '请输入用户名和密码';                    thisvue.errorFlag = true;                }                else{                    $.ajax({                        type:'POST',                        url:'../server/login.php',                        data:{                            userid:thisvue.userid,                            usercode:thisvue.usercode                        },                        success:function(res){                            if(res[0].code==1)                            {                                thisvue.errorFlag = false;                                _window.location.href="./homemobile.html";                            }                            else{                                thisvue.errorMsg = '账号或密码错误';                                thisvue.usercode = '';                                thisvue.errorFlag = true;                            }                        }                    })                }            }        }    })})
PHP
query($sql);                      $resArray = mysqli_fetch_array($result);                                       if($resArray["user_password"] == $mycode)                      {                                $_SESSION['chenzhe_user_id'] = $resArray['user_id'];                                $result_array[0] = ['code'=>'1','msg'=>'登陆成功'];                                echo json_encode($result_array);                      }                      else                      {                                $result_array[0] = ['code'=>'0','msg'=>'用户名或密码输入错误'];                                echo json_encode($result_array);                      }                        $conn->close();                }        }        else        {                $result_array[0] = ['code'=>'0','msg'=>'请输入用户名或密码'];                echo json_encode($result_array);        }        ?>
注册部分
HTML

{{errorMsg}}

电脑版 闽ICP备19008574号-1
Javascript
import $ from './js/jquery.js';import './css/mobilecommon.css';import Vue from '../node_modules/vue/dist/vue.js';$(function(){    var vm = new Vue({        el:"#app",        data:{            errorMsg:'',            errorFlag:0,            //填写注册信息            userid:'',            username:'',            usercode:'',            usercodes:'',            usermail:'',            //验证注册信息            code:'',            btnGetCode:0, //用于判断当前是否获取了一次验证码,默认是0,获取一次后改成1            testcode:0, //用于判断当前是否完成了验证码验证,默认是0,验证通过是1            clock:60,            testUserId:false,//检测当前用户名是否已经注册            testpass:false,//检测密码安全            testmail:false, //检测邮箱是否被注册过了        },        methods:{            testUserIdFunc(){ //检测用户名是否已经注册                var thisvue = this;                var testall = /^[a-zA-Z][a-zA-Z0-9]*$/; //只能是数字和字母                if(thisvue.userid=='')                {                    thisvue.testUserId = false;                    return 0;                }                else if(!testall.test(thisvue.userid)) //检测英文和数字                {                    this.errorFlag = 1;                    this.errorMsg = '用户名必须以英文开头,且只能由英文和数字组成';                }                else if(thisvue.userid.length<9)                {                    thisvue.errorFlag = 1;                    thisvue.errorMsg = '用户名长度须在9-16之间';                    thisvue.testUserId = false;                    return 0;                }                    else{                    $.ajax({                        type:'POST',                        url:'../server/testUserId.php',                        data:{                            user_id:thisvue.userid                        },                        success:function(res)                        {                            if(res.code==1)                            {                                thisvue.testUserId = true;                                thisvue.errorFlag = 0;                            }                            else{                                thisvue.testUserId = false;                                thisvue.errorFlag = 1;                                thisvue.errorMsg = res.msg;                            }                        }                    })                }            },            readInfo(){ //检索密码安全等                var result = 1;                var testall = /^(?!\d+$)[\da-zA-Z]+$/; //只能是数字和字母                if(this.usercode.length<9) //检测长度                {                    this.errorFlag = 1;                    this.errorMsg = '密码长度须在9-20个字符,只能由英文和数字组成';                    result = 0;                }                else if(!testall.test(this.usercode)) //检测英文和数字                {                    this.errorFlag = 1;                    this.errorMsg = '密码只能使用英文+数字,且不能为纯数字';                    result = 0;                                }                else if(this.usercode!=this.usercodes)                {                    this.errorFlag = 1;                    this.errorMsg = '两次密码输入不一致';                    result = 0;                                    }                /*else if(testenglish.test(this.usercode))                {                    this.errorFlag = 1;                    this.errorMsg = '密码不能为纯数字';                    result = 0;                }*/                                if(result==1)                {                    this.errorFlag = 0;                    this.testpass = 1;//如果密码验证成功,则通过                }                 return result;            },            register(){                var thisvue = this;                if(thisvue.usermail==''||thisvue.code=='')                {                    thisvue.errorMsg = '你还没有进行邮箱验证';                    thisvue.errorFlag = 1;                }                else{                    thisvue.verifyCode();                    $.ajax({                        url:'../server/register.php',                        type:'POST',                        data:$("#registerForm").serialize(),                        success:function(res)                        {                            if(res.code==1)                            {                                _window.location.href = 'indexmobile.html';                            }                            else{                                thisvue.errorMsg = '注册失败';                                thisvue.errorFlag = 1;                            }                        }                    })                }                            },            getCode(){ //获取验证码                if(this.userid==''||this.username==''||this.usercode==''||this.usercodes==''||this.usermail=='')                {                    this.errorFlag = 1;                    this.errorMsg = '请填写全部的信息后获取验证码';                }                else if(this.usercode!=this.usercodes)                {                    this.errorFlag = 1;                    this.errorMsg = '两次密码输入不一致';                }                else{                    var thisvue = this;                    thisvue.btnGetCode = 1; //把获取验证码按钮禁用                    var timer1 = setInterval(function(){thisvue.clock=thisvue.clock-1;},1000);                    setTimeout(function(){                        clearInterval(timer1);                        thisvue.btnGetCode=0;                        thisvue.clock=60;                    },60000);                    //发送邮件                    $.ajax({                        type:'POST',                        url:'../server/mail/sendMail.php',                        async:false,                        data:{                            address:thisvue.usermail                        },                        success:function(res)                        {                            if(res.code==1)                            {                                thisvue.errorFlag = 1;                                thisvue.errorMsg = '我们发送了一封邮件到你的邮箱,请尽快验证'                             }                        }                    })                }            },            verifyCode(){ //验证验证码                var thisvue =this;                if(thisvue.code>100000&&thisvue.code<999999)                {                    $.ajax({                        type:'POST',                        url:'../server/mail/verifyCode.php',                        data:{code:thisvue.code},                        success:function(res)                        {                            if(res.code=='1')                            {                                thisvue.testcode=1;                            }                            else{                                thisvue.errorFlag=1;                                thisvue.errorMsg='验证码不正确,请重新输入';                                return 0;                            }                        }                    })                }            },            testmailFunc(){                var thisvue = this;                if(this.usermail!=''&&this.usermail.indexOf('@')!='')                {                    $.ajax({                        type:'POST',                        url:'../server/testmail.php',                        data:{                            user_mail:thisvue.usermail                        },                        success:function(res){                            if(res.code==1)                            {                                thisvue.testmail = true;                                thisvue.errorFlag = 0;                            }                            else{                                thisvue.testmail = false;                                thisvue.errorFlag = 1;                                thisvue.errorMsg = '此邮箱已被注册,换个邮箱试试吧';                            }                        }                    })                }            }        }    })})
query($test);        if(mysqli_num_rows($testResult)==0)        {                $path="/home/www/htdocs/carelesswhisper/src/img/".$myid; //判断目录存在否,存在给出提示,不存在则创建目录                if (is_dir($path)){                        $result = ['code'=>'1','msg'=>'覆盖用户目录'];                 }                else{//第三个参数是"true"表示能创建多级目录,iconv防止中文目录乱码                        $res=mkdir(iconv("UTF-8", "GBK", $path),0777,true);                         $result = ['code'=>'1','msg'=>'注册成功'];                }                $conn->query(" INSERT INTO user_login VALUES('$myid','$mycode','$myname','$mymail','imageFile/image.jpg','未填','未填','未填','未填','0') ");                $conn->query("INSERT INTO personal_follow VALUES('$gm','$myid',1,'2019',0)");                $conn->query("INSERT INTO personal_follow VALUES('$myid','$gm',1,'2019',0)");                $conn->close();                        }        else{                $result = ['code'=>'0','msg'=>'此用户名已被使用'];         }        $_SESSION['code']='';        echo json_encode($result);        ?>

感谢各位的阅读,以上就是"vue加php如何实现登陆"的内容了,经过本文的学习后,相信大家对vue加php如何实现登陆这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0