zoukankan      html  css  js  c++  java
  • 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响应的内涵要点,本人菜鸟,不喜勿喷

  • 相关阅读:
    Mysql 时间字段(加上或者减去一段时间)
    awk数组与语法
    awk模块、变量、执行
    awk简介与表达式实例
    图数据库
    推荐几款优秀的开源博客系统
    红黑树
    用户画像基础概念
    Panda Global获悉,美国承诺4年内明确区块链数字资产监管方式!
    重庆聚焦区块链应用,Panda Global觉得春天真的来了!
  • 原文地址:https://www.cnblogs.com/cxl862002755/p/13222345.html
Copyright © 2011-2022 走看看