实例
index: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/22 0022 * Time: 下午 2:15 */ ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <?php include "includes/header.inc.php"; session_start(); //if (!empty($_COOKIE)) { if (!isset($_SESSION['username'])) { echo '<a href="login_page.php">登录</a>'; } else { echo '<a href="loginout.php">退出</a>'; } ?> </body> </html> login: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/22 0022 * Time: 下午 2:49 */ //加载公共函数库 require ('includes/function.php'); //连接数据库 require ('includes/connect.php'); session_start(); //验证登录 //$check=true/false; $data=['user_id'=>$user_id, 'user_name'=>$user_name]; list($check, $data) = check_login($conn, $_POST['username'], $_POST['password']); //验证通过 if ($check) { //设置cookies // setcookie('username', $data['dg_username']); // setcookie('password', $data['dg_password']); $_SESSION['username']=$data['dg_username']; $_SESSION['password']=$data['dg_password']; //跳转页面 jump_next('login_inside.php'); } else { //验证失败 $errors = $data; } //关闭数据库连接 mysqli_close($dbc); login_inside: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/22 0022 * Time: 下午 3:00 */ session_start(); //if (!isset($_COOKIE['username'])) { if (!isset($_SESSION['username'])) { require "includes/function.php"; //跳转到默认首页 jump_next(); } $page_title = '已经登录'; include ('includes/header.inc.php'); echo <<< "WELCOME" <h2 style="color:red">登陆成功</h2> <!--<p>欢迎您: {$_COOKIE['username']}</p>--> <p>欢迎您: {$_SESSION['username']}</p> <p><a href="loginout.php">退出</a></p> WELCOME; login_page: <?php /** * 登录页面并报告错误 * 设置当前页面的标题 * 在login.php中调用 */ $page_title = '用户登录'; //加载头部文件 include('includes/header.inc.php'); //打印错误信息 if (isset($errors) && !empty($errors)) { $errors_msg = '<p style="color:red">'; foreach ($errors as $msg) { $errors_msg .= $msg.'<br>'; } echo $errors_msg.'</p>'; } ?> <h2 style="color:red">用户登录</h2> <form action="login.php" method="post"> <p> <label for="username">用户:</label> <!--使用粘性表单技术在文本框中显示用户之前输入的内容,提升用户体验--> <input type="text" name="username" id="username" value="<?php echo isset($_POST['username'])?$_POST['username']:'' ?>"> </p> <p> <label for="password">密码:</label> <input type="password" name="password" id="password" value="<?php echo isset($_POST['password'])?$_POST['password']:'' ?>"> </p> <p><button type="submit" name="submit" id="submit">登录</button></p> </form> loginout: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/22 0022 * Time: 下午 3:33 */ include ('includes/header.inc.php'); session_start(); //if (!isset($_COOKIE['username'])) { if (!isset($_SESSION['username'])) { require ('includes/function.php'); //跳转到默认首页 jump_next(); } else { //删除cookies // setcookie('username', '', time()-3600*3); // setcookie('password','', time()-3600*3); unset($_SESSION['username']); unset($_SESSION['password']); } //设置页面标题 $page_title = '已经登录'; //打印退出信息,并提供登录功能 echo <<< "WELCOME" <h2 style="color:red">退出成功</h2> <p><a href="login_page.php">登录</a></p> WELCOME; connect: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/22 0022 * Time: 下午 2:37 */ //数据库连接 define("DB_HOST", "localhost"); define("DB_USER", "root"); define("DB_PAW", "root"); define("DB_NAME", "testguest"); $conn = @mysqli_connect(DB_HOST, DB_USER, DB_PAW, DB_NAME) or die("数据库连接失败"); //如上的@符号可以屏蔽当数据库连接失败的警告。只显示想要展示的错误信息 //mysqli_select_db($dbname) 如果在@mysqli_connect 中没有选择指定的数据库 那么使用mysqli_select_db($dbname) or die("指定数据库不存在"); mysqli_query($conn, "SET NAMES UTF8") or die("字符集错误");//这样设置可以 //$conn->set_charset("utf8") or die("字符集错误");//这样设置字符集也可以 function: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/22 0022 * Time: 下午 2:19 */ function jump_next($page = 'index.php'){ //默认url格式 $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); //如果有,去掉url右侧的斜线 $url = rtrim($url, '/\\'); //添加上当前的脚本名称,默认为:index.php $url .= '/'.$page; //跳转到指定目标地址 header('Location:'. $url); exit(); } function check_login($conn,$username,$password){ //验证用户名 if (empty($username)) { $errors[] = '用户名不能为空'; } else { //mysqli_real_escape_string():转义字符串的特殊字符 $e = mysqli_real_escape_string($conn, trim($username)); } //验证密码 if (empty($password)) { $errors[] = '密码不能为空'; } else { $p = mysqli_real_escape_string($conn, trim($password)); } if (empty($errors)){ $sql = "SELECT * FROM tg_user WHERE dg_username='$username'"; $res = mysqli_query($conn,$sql); echo "<pre>"; print_r($res); $row = mysqli_fetch_array($res,MYSQL_ASSOC); print_r($row); if ($row!=NULL){ return [true, $row]; }else{ return [FALSE ,"没有改用户"]; } } return [FALSE ,$errors]; } header.inc: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/4/22 0022 * Time: 下午 2:12 */ header("content-type:text/html;charset=utf-8"); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title> <?php echo isset($page_title) ? $page_title : '首页'; ?></title> </head> <body> <h3>头部</h3> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例