php7 tutorialThe column introduces how to develop a message board
Recommended (free): php7 tutorial
Foreword: It has not been updated for a while, so I will post the rest The tutorial is finished. It’s still the same as before, start typing code.
1. Super global variable $_SESSION
The application saves the login status. Note that session_start() must be used before use.
2. Give ordinary Add salt value to the password to strengthen the password
3. Log out and log in, check the logout.php code
html code
nbsp;html> <meta> <title>留言表单后台登录_科科分享</title> <link> <p> </p><p> </p><p> </p><p> </p><h3>留言表单后台登录</h3> <h5><span>FEEDBACK</span></h5> <p> </p>
css code
* { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; margin: 0; padding: 0; } *:before, *:after { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } h1, h2, h3, h3, h4, h5, h6 { font-weight: normal; } body { font-family:"微软雅黑", "Microsoft Yahei"; } .wp { width: 100%; max-width: 1170px; margin: 0 auto; } a { color: #333; } a:hover { color: #e4392a; } #hd { /*height: 101px;*/ height: 90px; background: #fff; box-shadow: 0px 2px 2px rgba(0, 0, 0, .3); position: fixed; width: 100%; left: 0; top: 0; z-index:9999; -o-transition:.3s; -ms-transition:.3s; -moz-transition:.3s; -webkit-transition:.3s; transition:.3s; } .tit-i { text-align: center; padding: 50px 0; line-height: 36px; overflow: hidden; } .tit-i h3 { font-size: 30px; color: #002; margin-bottom: 10px; } .tit-i h5 { font-size: 20px; color: #cacace; text-transform: uppercase; font-family: Arial; } .tit-i h5 span { color: #e4392a; } .tit-i em { width: 45px; border-bottom: 1px solid #7f7f90; display: inline-block; } .login-l { width: 38%; float: left; } .login-r { width: 50%; margin: 0 auto; } .login-form .inp { width: 49%; border: 1px solid #6d6d88; height: 33px; padding: 0 10px; margin-bottom: 15px; } .login-form textarea { width: 100%; border: 1px solid #6d6d88; height: 98px; margin-bottom: 24px; } .login-form .sub { width: 100%; height: 40px; line-height: 40px; text-align: center; color: #fff; background-color: #63637f; border: 0; cursor: pointer; font-family:"微软雅黑", "Microsoft Yahei"; font-size: 16px; } .list ul{ padding: 20px 0; width: 50%; margin: 0 auto; } .list .tit-i{ padding: 50px 0 10px 0; } .list ul li{line-height: 1.8em;color: #666;}
-- -- 表的结构 `admin` -- CREATE TABLE `admin` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `login_name` varchar(128) NOT NULL COMMENT '登录账号', `password` varchar(64) NOT NULL COMMENT '登录密码', `salt` char(4) NOT NULL COMMENT '盐值,加强密码强度', `addtime` int(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '记录时间', PRIMARY KEY (`id`), KEY `login_name` (`login_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='管理员数据表';
login.php
<?php include 'config.php'; $login_name = !empty($_POST['login_name'])? addslashes(strip_tags($_POST['login_name'])):''; $password = !empty($_POST['password'])?addslashes(strip_tags($_POST['password'])):''; // 简单验证一下提交的数据 if (empty($login_name) || empty($password)) { exit('请输入登录信息!'); } $sql = "SELECT * FROM admin WHERE `login_name` = '{$login_name}' LIMIT 1"; $result = mysqli_query($mysqli, $sql); $row = mysqli_fetch_array($result); if (empty($row)) { exit('账号密码错误!'); } $res_password = $row['password']; $password = md5(md5($password) . $row['salt']); // 密码不匹配 if ($res_password != $password) { exit('账号密码错误!'); // 为了程序的安全,这里不必具体提示密码错误,告知用户账号或密码错误即可。 } // 登录成功,保存登录信息,一般都用session进行处理 session_start(); $_SESSION['login_admin_id'] = $row['id']; $_SESSION['login_admin_name'] = $row['login_name']; echo "<script type='text/javascript'>alert('登录成功');setTimeout(function(){location.href='/login_ok.php'}, 500)";
login_ok.php
<?php session_start(); include 'php/config.php'; ?> nbsp;html> <meta> <title>留言表单后台登录_科科分享</title> <link> <p> </p><p> </p><p> </p><p> 您好,<?php echo $_SESSION['login_admin_name']?>,<a>退出</a>。 </p> <p> </p><h3>您已登录后台中心!</h3>
<?php session_start(); unset($_SESSION['login_admin_id']); unset($_SESSION['login_admin_name']); session_destroy(); echo "<script type='text/javascript'>alert('注销登录成功');setTimeout(function(){location.href='/login.html'}, 500)";
Develop a good habit, simply test it once, and then submit the work.
Login page
The page after successful login
4. Summary
Knowledge points of this section Just one session, grasp the usage, and you are not far from the high-end cloud backend.
Remember to use your hands to type code, blindly copying and pasting will not achieve the learning effect~
Related free learning recommendations: php programming (video)
The above is the detailed content of Record PHP7 message board development background management-login/logout. For more information, please follow other related articles on the PHP Chinese website!