PHP實現用戶登陸頁面

PHP學習平常,放在上面記錄一下咯
我用了bootstrap框架,這樣的界面要好看一點
登陸頁面:
image.png
必須用戶名、密碼、驗證碼都輸入正確才能登陸成功喔,不然出現下面提示
image.png
image.png
登錄成功以後,登陸和註冊選項切換爲用戶暱稱和註銷選項:
image.png
登錄成功以後:
登陸成功以後
選擇註銷:
在這裏插入圖片描述 在這裏插入圖片描述
HTML代碼:php

<link rel="stylesheet" href="css/bootstrap.css">
    <style>
          li.toggle{
              display: block;
           }
          li.toggle1{
              display: none;
            }
        </style>
        <li class="toggle"><a data-toggle="modal" href="#loginer">登陸</a></li>
        <li class="toggle"><a data-toggle="modal" href="#register">註冊</a></li>
        <li class="toggle1"><a id="nicheng" href="##"></a></li>
        <li class="toggle1"><a id="logout" href="##">註銷</a></li>
//模態框
<div id="loginer" class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">請登陸</h4>
            </div>
            <form id="form_login" class="form-horizontal">
                <div class="modal-body">
                    <div class="form-group has-feedback">
                        <label class="col-sm-3 control-label" for="zh1">用戶名:</label>
                        <div class="col-sm-9 require">
                            <input type="text" class="form-control" name="username" id="zh1"
                                   pattern="\w{6,10}" required>
                        </div>
                    </div>
                    <div class="form-group has-feedback">
                        <label class="col-sm-3 control-label" for="pw1">密碼:</label>
                        <div class="col-sm-9 require">
                            <input type="password" pattern="\w{6,10}" class="form-control" name="pwd" id="pw1"
                                   required>
                        </div>
                    </div>
                    <div class="form-group has-feedback">
                        <label class="col-sm-3 control-label" for="yzm">驗證碼:</label>
                        <div class="col-sm-5 require">
                            <input type="text" pattern="\d{4}" class="form-control" name="yzm" id="yzm"
                                   required>
                        </div>
                        <div class="col-sm-4">
                            <img src="php/idcode.php" alt="">
                        </div>
                    </div>
                    <div class="form-group has-feedback">
                        <label id="info" class="col-sm-4 control-label pull-left"></label>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">登陸</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">關閉</button>
                </div>
            </form>
        </div>
    </div>
</div>

JS代碼:css

<script src="js/jquery-2.1.3.js"></script>
<script src="js/bootstrap.min.js"></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(鏈接數據庫):html

<?php
$link=new PDO("mysql:host=localhost;port=3306;dbname=db","root","");
$link->query("set names utf8");

get.php(獲取用戶登陸信息):mysql

<?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(登陸):jquery

<?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(註銷):sql

<?php
session_start();
unset($_SESSION['username']);//銷燬用戶名
unset($_SESSION['nc']);//銷燬暱稱
echo json_encode(true);//返回結果

上面登陸頁面的驗證碼在個人另外一篇文裏面,須要的小夥伴能夠看一下:
https://www.jianshu.com/p/7473f5bdd9c0數據庫

相關文章
相關標籤/搜索