您好,登錄后才能下訂單哦!
這篇文章主要講解了“vue加php如何實現登陸”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“vue加php如何實現登陸”吧!
vue加php實現登錄的方法:1、創建登錄部分的代碼文件;2、創建Javascript代碼;3、在vue項目中使用PHP來做用戶的注冊和登錄功能即可。
本文操作環境:Windows7系統,PHP7.1版,Dell G3電腦。
對于一款應用來說,最基本的就是用戶的注冊和登錄功能,這篇博客就總結一下在vue項目中如何使用PHP來做用戶的注冊和登錄功能。
<div id="app" class="container"> <div style="text-align:center;margin-top:60px;"> <img src="../src/icon/vlogo.png" style="width:20rem;height:auto;" alt=""> </div> <form class="form middle"> <br> <div class="form-group"> <input type="text" class="form-control input100" v-model="userid" placeholder="用戶名 / 郵箱"/> </div> <div class="form-group"> <input type="password" class="form-control input100" v-model="usercode" placeholder="密碼" @keyup.13="login" /> </div> <label class="errorMsg" v-if="errorFlag" v-cloak >{{ errorMsg }}</label> <div class="form-group btn100"> <button type="button" @click="login" class="btn btn-primary btn100">登陸</button> </div> <div class="form-group btn100"> <a href="./registermobile.html" class="btn btn-default btn100">注冊</a> </div> <h7 style="text-align:right;"> <a href="./findpassword.html" style="margin-right:2rem;">找回密碼</a> </h7> <hr> <h7 style="text-align:center;margin-top:3rem;"> <a style="margin-right:2rem;">? vchenzhe</a> <a href='http://www.beian.gov.cn' style="text-decoration:none;color:black;" target='_blank'>閩ICP備19008574號-1</a> </h7> </form> </div>
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 session_start(); header('Content-Type:application/json; charset=utf-8'); $myid = $_POST[userid]; $mycode = md5($_POST[usercode]); if($myid!=''&&$mycode!='') { $conn = new mysqli("localhost:3306", "root", "", "personal"); if ($conn != null) { $sql = "select * FROM user_login where user_id='$myid' or user_mail = '$myid' "; $result =$conn->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); } ?>
<div class="container" style="margin-top:2rem;" id="app"> <ol class="breadcrumb btn100"> <li><a href="./indexmobile.html">返回</a></li> <li class="active">注冊</li> </ol> <p class="errorMsg" v-if="errorFlag==1" v-cloak >{{errorMsg}}</p> <form class="form" id="registerForm"> <div class="form-group has-feedback"> <input type="text" @keyup="testUserIdFunc" v-model="userid" name="userid" minlength=9 maxlength=16 class="form-control input100" placeholder="用戶名" required> <span v-show="testUserId" class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true" style="color:#5cb85c;"></span> </div> <div class="form-group"> <input type="text" v-model="username" name="username" maxlength=10 class="form-control input100" placeholder="昵稱" required> </div> <div class="form-group has-feedback "> <input type="password" @keyup="readInfo" v-model="usercode" name="usercode" minlength=9 maxlength=20 class="form-control input100" placeholder="密碼" required> <span v-show="testpass" class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true" style="color:#5cb85c;"></span> </div> <div class="form-group has-feedback "> <input type="password" @keyup="readInfo" v-model="usercodes" name="checkusercode" maxlength=20 class="form-control input100" placeholder="確認密碼" required> <span v-show="testpass" class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true" style="color:#5cb85c;"></span> </div> <div class="form-group has-feedback "> <input type="email" @keyup="testmailFunc" v-model="usermail" name="usermail" class="form-control input100" placeholder="郵箱" required> <span v-show="testmail" class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true" style="color:#5cb85c;"></span> </div> <div class="form-group btn100" style="display:flex;" > <input type="number" v-model="code" class="form-control" placeholder="驗證碼" required> <button v-if="testUserId==0||testpass==false||usermail==''||testmail==false" type="button" class="btn btn-default btn80 btn-disabled" disabled style="margin-left:1rem;">獲取驗證碼</button> <button v-show="btnGetCode==0" v-if="testUserId==1&&testpass==true&&usermail!=''&&testmail==true" type="button" class="btn btn-default btn80" @click="getCode" style="margin-left:1rem;">獲取驗證碼</button> <button v-show="btnGetCode==1" type="button" class="btn btn-disabled btn80" disabled style="margin-left:1rem;">已發送({{ clock }}s)</button> </div> <div class="btn100"> <button type="button" class="btn btn-primary btn100" @click="register">注冊</button> </div> </form> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h5 class="modal-title" id="myModalLabel">消息</h5> </div> <div class="modal-body"> {{ errorMsg }} </div> <div class="modal-footer"> <button type="button" class="btn btn-primary" data-dismiss="modal">確認</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal --> </div> <h7 style="text-align:center;margin-top:3rem;"> <a href="./index.html" style="margin-right:2rem;">電腦版</a> <a href='http://www.beian.gov.cn' style="text-decoration:none;color:black;" target='_blank'>閩ICP備19008574號-1</a> </h7> </div>
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 = '此郵箱已被注冊,換個郵箱試試吧'; } } }) } } } })})
<?php session_start(); header('Content-Type:application/json; charset=utf-8'); $myid = $_POST[userid]; $mycode = md5($_POST[usercode]); $myname = $_POST[username]; $mymail = $_POST[usermail]; $gm = 'vchenzhecom'; $conn = new mysqli("47.106.190.129:3306", "root", "52F7cbad94f2", "personal"); $test = "SELECT * FROM user_login WHERE `user_id` = '$myid'"; $testResult = $conn->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如何實現登陸這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。