设为首页 加入收藏

TOP

Bootstrap 结合 PHP ,做简单的登录以及注册界面及功能(一)
2019-08-15 23:29:50 】 浏览:55
Tags:Bootstrap 结合 PHP 简单 登录 以及 注册 界面 功能

登录实现

HTML代码

<div class="container">
        <?php if (isset($error_msg)): ?>
        <div class="alert alert-danger" role="alert"><?php echo $error_msg; ?></div>
        <?php endif ?>
        <?php if (isset($success_msg)): ?>
        <div class="alert alert-info" role="alert"><?php echo $success_msg; ?></div>
        <?php endif ?>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" autocomplete="on">
            <div class="form-group">
                <label for="administrator">管理员账号(11位手机号码)</label>
                <input type="number" class="form-control" name="administrator" id="administrator" value="<?php echo isset($_POST['administrator']) ? $_POST['administrator'] : ''; ?>">
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" class="form-control" name="password" id="password">
            </div>
            <button class="btn btn-info btn-md btn-block">登录</button>
        </form>
    </div>

CSS:

<link rel="stylesheet" type="text/css" href="css/Bootstrap.css">
    <style type="text/css">
        .container {
            margin-top: 150px;
        }
    </style>

PHP:

function login () {
    if (empty($_POST['administrator'])) {
        $GLOBALS['error_msg'] = '请输入管理员账号';
        return;
    }
    if (strlen($_POST['administrator']) !== 11) {
        $GLOBALS['error_msg'] = '您输入的管理员账号不符合相关规定';
        $_POST['administrator'] = '';
        return;
    }
    if (empty($_POST['password'])) {
        $GLOBALS['error_msg'] = '请输入密码';
        return;
    }

    $connection = mysqli_connect('localhost', 'root', '密码', 'students_info_system');

    if (!$connection) {
        $GLOBALS['error_msg'] = '连接数据库失败';
        return;
    }

    $query = mysqli_query($connection, 'select * from administrators');

    if (!$query) {
        $GLOBALS['error_msg'] = '查询数据失败';
        return;
    }

    while ($administrator = mysqli_fetch_assoc($query)) {
        $administrators[] = $administrator;
    }
    // var_dump($administrators);

    for ($i = 0; $i < count($administrators); $i++) {
        $administrator[] = $administrators[$i]['administrator'];
        $passwords[] = $administrators[$i]['password'];
    }

    // var_dump($administrator);
    // var_dump($passwords);

    if (!in_array((string)$_POST['administrator'], $administrator)) {
        $GLOBALS['error_msg'] = '您输入的管理员账号不存在';
        return;
    }

    if (!in_array((string)$_POST['password'], $passwords)) {
        $GLOBALS['error_msg'] = '密码错误';
        return;
    }

    // 账号和密码都存在了----判断密码和账号与数据库中是否一致
    $index = array_search($_POST['administrator'], $administrator);
    if ((string)$_POST['password'] !== $passwords[$index]) {
        $GLOBALS['error_msg'] = '密码错误';
        return;
    }

    // 在数据库找到了相应的账号和密码
    $GLOBALS['success_msg'] = '登录成功';

    // 延时2秒执行后面的代码
    time_sleep_until(time() + 2);

    // 跳转到内容页面(并将账号一并传过去,区分用户账号以显示各个用户对应的界面)
    header("Location: student_info.php?id={$_POST['administrator']}");
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    login();
}

注册实现

HTM

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇错误处理 下一篇文件系统操作

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目