Bootstrap 结合 PHP ,做简单的登录以及注册界面…

2019-05-18 07:10:43来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

登录实现

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();
}

注册实现

HTML:

<div class="container">
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" complete="on">
            <?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 ?>
            <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" name="password" id="password" class="form-control" value="<?php echo isset($_POST['password']) ? $_POST['password'] : ''; ?>">
            </div>
            <div class="form-group">
                <label for="check-pwd">确认密码</label>
                <input type="password" name="check-pwd" id="check-pwd" class="form-control">
            </div>
            <button class="btn btn-info btn-block btn-md">注册</button>
        </form>
    </div>

CSS:

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

PHP:

function register () {
    $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'];
    }

    if (empty($_POST['administrator'])) {
        $GLOBALS['error_msg'] = '请输入管理员账户';
        return;
    }
    if (strlen($_POST['administrator']) !== 11) {
        $GLOBALS['error_msg'] = '您输入的管理员账户不符合相关规定,请重新输入';
        $_POST['administrator'] = '';
        return;
    }
    // 判断该账号是否已被注册
    if (in_array($_POST['administrator'], $administrator)) {
        $GLOBALS['error_msg'] = '该账号已被注册,请另起账号';
        return;
    }
    if (empty($_POST['password'])) {
        $GLOBALS['error_msg'] = '请输入密码';
        return;
    }
    if (empty($_POST['check-pwd'])) {
        $GLOBALS['error_msg'] = '请确认密码';
        return;
    }
    if ($_POST['check-pwd'] !== $_POST['password']) {
        $GLOBALS['error_msg'] = '两次密码输入不一致,请重新确认密码';
        return;
    }

    $administrator = $_POST['administrator'];
    $password = $_POST['password'];$query = mysqli_query($connection, "insert into administrators values ({$administrator}, '{$password}')");

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

    $affected_rows = mysqli_affected_rows($query);

    if ($affected_rows !== 1) {
        $GLOBALS['error_msg'] = '插入数据失败';
    }

    $GLOBALS['success_msg'] = '注册成功';

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

    header('Location: index.html');
    
}

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

 


原文链接:https://www.cnblogs.com/duxiu-fang/p/10883300.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:文件系统操作

下一篇:PSR - PHP标准推荐