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

本文操作环境:Windows7系统,PHP7.1版,Dell G3电脑。
Vue + PHP 做用户注册登录功能
对于一款应用来说,最基本的就是用户的注册和登录功能,这篇博客就总结一下在vue项目中如何使用PHP来做用户的注册和登录功能。
登录部分:
HTML
<div>
<div>
@@##@@
</div>
<form>
<br>
<div>
<input>
</div>
<div>
<input>
</div>
<label>{{ errorMsg }}</label>
<div>
<button>登陆</button>
</div>
<div>
<a>注册</a>
</div>
<h6>
<a>找回密码</a>
</h6>
<hr>
<h6>
<a>© vchenzhe</a>
<a>闽ICP备19008574号-1</a>
</h6>
</form>
</div>
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
<?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);
}
?>
注册部分
HTML
<div>
<ol>
<li><a>返回</a></li>
<li>注册</li>
</ol>
<p>{{errorMsg}}</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20e352aa140.jpg" alt="Build MCP servers and clients for PHP applications with the official PHP SDK" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="overflowclass">Build MCP servers and clients for PHP applications with the official PHP SDK</a>
<p class="overflowclass">使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<form>
<div>
<input>
<span></span>
</div>
<div>
<input>
</div>
<div>
<input>
<span></span>
</div>
<div>
<input>
<span></span>
</div>
<div>
<input>
<span></span>
</div>
<div>
<input>
<button>获取验证码</button>
<button>获取验证码</button>
<button>已发送({{ clock }}s)</button>
</div>
<div>
<button>注册</button>
</div>
</form>
<div>
<div>
<div>
<div>
<button>×</button>
<h4>消息</h4>
</div>
<div>
{{ errorMsg }}
</div>
<div>
<button>确认</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal -->
</div>
<h6>
<a>电脑版</a>
<a>闽ICP备19008574号-1</a>
</h6>
</div>
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.length100000&&thisvue.code<pre class="brush:php;toolbar:false;"><?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);
?>
推荐学习:《PHP视频教程》
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










