php實現登錄注冊界面


php實現登錄注冊界面

首先你要搭建一個自己的數據庫

我用wamp64創了一個people的數據庫

具體操作可以參考該搭建鏈接

https://blog.csdn.net/qq_36934826/article/details/78170855

 

這里就講下我實現的功能代碼:

創建sql.func.php實現一些基本函數功能

 

<?php

/**

*彈框

*/

function _alert($_info){

    echo "<script type='text/javascript'>alert('$_info');history.back();</script>";

    exit;

}

   

/**

 * _location():彈出一個對話框並且轉跳到另一個界面

 * @access public

 * @param string $_info 對話框上顯示的信息

 * @param string $_url  轉跳的頁面地址

 * @return void

 */

function _location($_info,$_url){

    if($_info==null){

        header('Location:'.$_url);

    }else{

        echo "<script type='text/javascript'>alert('$_info');location.href='$_url';</script>";

        exit;

    }

   

}

   

/**

 * _connect():連接數據庫

 * @access public

 * @return void

 */

function _connect()

{

    //定義全局變量$_conn,在函數外部也能調用

    global $_conn;

    $_conn=mysqli_connect(DB_HOST, DB_USER,DB_PWD);

    if (!$_conn) {

        exit('數據庫連接失敗:'.mysqli_error($_conn));

    }

}

   

/**

 * _select_db():選擇數據庫

 * @access public

 * @return void

 */

function _select_db(){

    global $_conn;

    if(!mysqli_select_db($_conn,DB_NAME)){

        exit('找不到數據庫'.mysqli_error($_conn));

    }

}

   

/**

 * _set_names():設置字符編碼

 * @access public

 * @return void

 */

function _set_names(){

    global $_conn;

    if(!mysqli_query($_conn,'SET NAMES UTF8')){

        exit('字符編碼錯誤'.mysqli_error($_conn));

    }

}

   

/**

 * _query():執行sql語句

 * @access public

 * @param string $_sql sql操作語句

 * @return string 返回結果集

 */

function _query($_sql){

    global $_conn;

    if(!$result=mysqli_query($_conn,$_sql)){

        exit('SQL執行失敗'.mysqli_error($_conn).mysqli_errno($_conn));

    }

    return $result;

}

   

/**

 * _fetch_array():根據sql語句遍歷數據庫。返回一個數組,鍵名是數據庫的表單結構名

 * @access public

 * @param string $_sql sql操作語句

 * @return array|null

 */

function _fetch_array($_sql){

    return mysqli_fetch_all(_query($_sql),MYSQLI_ASSOC);

}

   

/**

 * _num_rows():返回數據庫中查找條件的數據個數

 * @access public

 * @param string $_sql sql操作語句

 * @return int 返回數據個數

 */

function _num_rows($_sql){

    return mysqli_num_rows(_query($_sql));

}

   

/**

 * _affected_rows():返回數據庫里被影響到的數據條數

 * @access public

 * @return int 返回影響到的記錄數

 */

function _affected_rows(){

    global $_conn;

    return mysqli_affected_rows($_conn);

}

   

/**

 * _is_repeat():判斷數據在數據庫里是否已經存在

 * @access public

 * @param string $_sql sql操作語句

 * @param string $_info 彈窗上顯示的文字

 * @return void

 */

function _is_repeat($_sql,$_info){

    if(_fetch_array($_sql)){

        _alert_back($_info);

    }

}

   

/**

 * _close():關閉數據庫

 * @access public

 */

function _close(){

    global $_conn;

    if(!mysqli_close($_conn)){

        exit('數據庫關閉異常'.mysqli_error($_conn));

    }

}

?>

 

 

connect.php 實現數據庫的連接功能

<?php

    $_conn=mysqli_connect('localhost','root','');

    if (!$_conn) {

        exit('數據庫連接失敗:'.mysqli_error($_conn));

    }

    mysqli_select_db($_conn,'people')or die('找不到數據庫:'.mysqli_error($_conn).mysqli_errno($_conn));

    mysqli_query($_conn,"SET NAMES UTF8");

    // var_dump($_conn);

    include "sql.func.php";

 ?>

 

 

login.php實現登錄響應操作

<?php

include "./connect.php";

//接收數據

if(isset($_POST['register']))

{

    _location('歡迎注冊','register.php');

}

if(isset($_POST['userid']) && isset($_POST['password'])){

    //從數據庫里查找用戶名是否存在

    $_sql = "SELECT user_id,user_password FROM user WHERE user_id='{$_POST['userid']}'";

    $result = _fetch_array($_sql);

    if(!empty($result[0])){

        if($result[0]['user_password']==$_POST['password']){

            _location('登錄成功','https://www.cnblogs.com/cxl862002755/');

        }else{

            _alert('密碼錯誤');

        }

    }else{

        _alert('用戶名不存在');

    }

    _close();

    exit;

}

?>

 

 

register.php實現注冊響應操作

 

<?php

include "./connect.php";

 

if(isset($_POST['index'])) _location("","index.html");

//接收數據

if(isset($_POST['userid']) && isset($_POST['password'])){

    $_userid=$_POST['userid'];

    $_password=$_POST['password'];

    if($_userid =='' || $_password == ''_location("用戶名和密碼不能為空","register.php");

    //插入到數據庫中

    $_sql = "INSERT INTO user(user_id,user_password)values('{$_POST['userid']}','{$_POST['password']}')";

    $_result = _query($_sql);

    _location("注冊成功!","index.html");

    _close();

    exit;

}else  

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>注冊</title>

    <STYLE type="text/css">

            #register {

                width600px;

                height280px;

                positionabsolute;

                left50%;

                top50%;

                colorred;

                font-size20px;

                font-weight600;

                margin-left-300px;

                margin-top-140px;

                border1px;

                background-colorred;

                background-imageurl(http://img0.imgtn.bdimg.com/it/u=1999267794,2294725296&fm=26&gp=0.jpg);

            }

              

            #form {

                width400px;

                height160px;

                positionrelative;

                left50%;

                top50%;

                margin-left-200px;

                margin-top-80px;

            }

              

            label {

                width70px;

                displayinline-flex;

                height30px;

            }

            body{

                background-imageurl(http://images2.china.com/tech/zh_cn/news/product/891/20091209/2009120916491939987300.jpg);

                background-sizecover;

            }

        </STYLE>

</head>

<body>

    <div id="register">

        <div id="form">

        <form action="register.php" method="post">

        <ul>

            <li>用戶名:<input type="text" name="userid"></li>

            <li>&nbsp&nbsp :<input type="password" name="password"></li>

            <li style="list-style-type: none;">

            <label for=""></label>

            <input type="submit" value="注冊">

            <input type="submit" name="index" value="返回">

        </li>

        </ul>

          

          

          

    </form>

        </div>

    </div>

      

</body>

</html>

 

 

index.html最終頁面展示效果網頁

<!DOCTYPE html>

 

<body>

    <!DOCTYPE html>

    <html lang="en">

 

    <head>

        <meta charset="UTF-8">

        <title>登錄</title>

        <STYLE type="text/css">

            #login {

                width600px;

                height280px;

                positionabsolute;

                left50%;

                top50%;

                colorred;

                font-size20px;

                font-weight600;

                margin-left-300px;

                margin-top-140px;

                border1px;

                background-colorred;

                background-imageurl(http://img0.imgtn.bdimg.com/it/u=1999267794,2294725296&fm=26&gp=0.jpg);

            }

              

            #form {

                width400px;

                height160px;

                positionrelative;

                left50%;

                top50%;

                margin-left-200px;

                margin-top-80px;

            }

              

            label {

                width70px;

                displayinline-flex;

                height30px;

            }

              

            body {

                background-imageurl(https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1593606498135&di=c7b87f8530b0f0c3b60f9eddf7ea7ad0&imgtype=0&src=http%3A%2F%2Fgss0.baidu.com%2F-4o3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F7e3e6709c93d70cf0f2918ebfcdcd100bba12b00.jpg);

                background-sizeauto;

            }

        </STYLE>

    </head>

 

    <body>

        <div id="login">

            <div id="form">

                <form action="login.php" method="post">

                    <fieldset>

                        <legend>用戶登錄</legend>

                        <ul>

                            <li>

                                <label>用戶名:</label>

                                <input type="text" name="userid">

                            </li>

                            <li>

                                <label>  :</label>

                                <input type="password" name="password">

                            </li>

                            <li style="list-style-type: none;">

                                <label> </label>

                                <input type="submit" name="login" value="登錄">

                                <input type="submit" name="register" value="注冊">

 

                            </li>

                        </ul>

                    </fieldset>

                </form>

            </div>

        </div>

 

    </body>

 

    </html>

 

</body>

 

部分效果圖

 

個人總結:個人也是一知半解,還是需要去吃透這些代碼,理解php響應的內涵要點,本人菜鳥,不喜勿噴


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM