Home >Backend Development >PHP Problem >How to use php to implement user login
php method to implement user login: 1. Create a get.php file to obtain user login information; 2. Create a login.php file to implement the login function; 3. Create a logout.php file. It can be used to implement the user logout function.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to use php to achieve user login?
Put it on your daily PHP learning routine to record it
I used the bootstrap framework, so this interface should look better
Login page:
You must enter the correct username, password, and verification code to successfully log in. Otherwise, the following prompt will appear
After successful login, the login and registration options switch to User nickname and logout options:
After successful login:
Select logout:
<link> <style> li.toggle{ display: block; } li.toggle1{ display: none; } </style>
<script></script> <script></script> <script> function toggle(){ $.getJSON("php/get.php",function (res) { if(res['flag']){ $('li.toggle').hide(); $('li.toggle1').show(); $('#nicheng').html(res['nc']) }else { $('li.toggle').show(); $('li.toggle1').hide(); } }); } toggle(); $('#form_login').submit(function (e) { e.preventDefault(); var data=$('#form_login').serialize(); $.getJSON('php/login.php',data,function (res) { if(res==3){ toggle(); $('#loginer').modal('hide'); }else if(res==2){ $('#info').html('用户名或密码有误') }else { $('#info').html('验证码有误') } }) }); $('#logout').click(function () { confirm('确定要注销?'); $.getJSON('php/logout.php',function (res) { if(res)toggle(); }) }) </script>connect.php (connect to database):
<?php $link=new PDO("mysql:host=localhost;port=3306;dbname=db","root",""); $link->query("set names utf8");get.php (get user login information):
<?php include_once ("connect.php"); session_start();/*开启会话*/ if (isset($_SESSION['username'])){ /* 判断用户会话里用户名是否存在,即用户是否登录*/ $json['nc']=$_SESSION['nc'];/*把昵称存起来,一会返回给首页*/ $json['flag']=true;/*用户已经登录,标志flag为true*/ }else $json['flag']=false;/*用户已经登录,标志flag为true*/ echo json_encode($json);/*返回json*/login.php (login):
<?php include_once ("connect.php"); session_start();/*开启会话*/ $user=$_GET['username'];/*获取登录表单提交过来的数据*/ $pwd=$_GET['pwd']; $yzm=$_GET['yzm']; if($yzm==$_SESSION['vCode']){/*当用户输入的验证码和图片验证码相同时*/ $result=$link->query("select * from `user` where username='$user' and pwd='$pwd'"); $link = null; $row = $result->fetch();/*读取从数据库获取的数据*/ if ($row) {/*如果数据存在,即用户登录成功*/ $_SESSION['username'] = $row['username']; /*将用户名和昵称存在服务器,可以多个页面使用*/ $_SESSION['nc'] = $row['nc']; $flag=3; }else{/*用户名或密码错误*/ $flag=2; } }else{/*验证码输入错误*/ $flag=1; } echo $flag;logout.php (logout):
<?php session_start(); unset($_SESSION['username']);//销毁用户名 unset($_SESSION['nc']);//销毁昵称 echo json_encode(true);//返回结果Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to use php to implement user login. For more information, please follow other related articles on the PHP Chinese website!