Bootstrap 结合 PHP ,做简单的登录以及注册界面…
2019-05-18 07:10:43来源:博客园 阅读 ()
登录实现
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标准推荐
- PHP写UltraEdit插件脚本实现方法 2020-03-29
- php 带逗号千位符数字的处理方法 2020-03-28
- PHP三元运算符的结合性介绍 2020-03-28
- PHP静态延迟绑定和普通静态效率的对比 2020-03-28
- 基于php流程控制语句和循环控制语句 2020-03-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash