這篇文章主要講解了“vue加php如何實現(xiàn)登陸”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“vue加php如何實現(xiàn)登陸”吧!
在達(dá)孜等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計,營銷型網(wǎng)站建設(shè),外貿(mào)網(wǎng)站制作,達(dá)孜網(wǎng)站建設(shè)費用合理。
vue加php實現(xiàn)登錄的方法:1、創(chuàng)建登錄部分的代碼文件;2、創(chuàng)建Javascript代碼;3、在vue項目中使用PHP來做用戶的注冊和登錄功能即可。
本文操作環(huán)境:Windows7系統(tǒng),PHP7.1版,Dell G3電腦。
對于一款應(yīng)用來說,最基本的就是用戶的注冊和登錄功能,這篇博客就總結(jié)一下在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="確認(rèn)密碼" 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;">已發(fā)送({{ 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">確認(rèn)</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, //用于判斷當(dāng)前是否獲取了一次驗證碼,默認(rèn)是0,獲取一次后改成1 testcode:0, //用于判斷當(dāng)前是否完成了驗證碼驗證,默認(rèn)是0,驗證通過是1 clock:60, testUserId:false,//檢測當(dāng)前用戶名是否已經(jīng)注冊 testpass:false,//檢測密碼安全 testmail:false, //檢測郵箱是否被注冊過了 }, methods:{ testUserIdFunc(){ //檢測用戶名是否已經(jīng)注冊 var thisvue = this; var testall = /^[a-zA-Z][a-zA-Z0-9]*$/; //只能是數(shù)字和字母 if(thisvue.userid=='') { thisvue.testUserId = false; return 0; } else if(!testall.test(thisvue.userid)) //檢測英文和數(shù)字 { this.errorFlag = 1; this.errorMsg = '用戶名必須以英文開頭,且只能由英文和數(shù)字組成'; } 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]+$/; //只能是數(shù)字和字母 if(this.usercode.length<9) //檢測長度 { this.errorFlag = 1; this.errorMsg = '密碼長度須在9-20個字符,只能由英文和數(shù)字組成'; result = 0; } else if(!testall.test(this.usercode)) //檢測英文和數(shù)字 { this.errorFlag = 1; this.errorMsg = '密碼只能使用英文+數(shù)字,且不能為純數(shù)字'; 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 = '密碼不能為純數(shù)字'; result = 0; }*/ if(result==1) { this.errorFlag = 0; this.testpass = 1;//如果密碼驗證成功,則通過 } return result; }, register(){ var thisvue = this; if(thisvue.usermail==''||thisvue.code=='') { thisvue.errorMsg = '你還沒有進(jìn)行郵箱驗證'; 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 = '請?zhí)顚懭康男畔⒑螳@取驗證碼'; } 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); //發(fā)送郵件 $.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 = '我們發(fā)送了一封郵件到你的郵箱,請盡快驗證' } } }) } }, 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; //判斷目錄存在否,存在給出提示,不存在則創(chuàng)建目錄 if (is_dir($path)){ $result = ['code'=>'1','msg'=>'覆蓋用戶目錄']; } else{//第三個參數(shù)是“true”表示能創(chuàng)建多級目錄,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如何實現(xiàn)登陸”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對vue加php如何實現(xiàn)登陸這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
網(wǎng)站名稱:vue加php如何實現(xiàn)登陸
文章路徑:http://chinadenli.net/article2/joiiic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站建設(shè)、品牌網(wǎng)站制作、云服務(wù)器、做網(wǎng)站、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)