实例 demo1.php
<?php /** * session会话 * 开启会话 * session_start(); */ $_SESSION['user_name'] = 'admin'; $_SESSION['user_id'] = '1'; echo $_SESSION['user_name']; //user_name|s:5:"admin";user_id|i:1 //删除会话 //单个会话 unset($_SESSION['user_name']); //user_id|i:1; //清空当前会话 $_SESSION = []; //干掉文件 //session_destroy(); setcookie('PHPSESSID','',time()-3600); //user_id|s:1:"1";user_name|s:5:"admin";
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例 index.php
<?php //想浏览器生成一个COOKIE = PHPSESSID session_start(); $page_title = '首页'; //导入头部 include('inc/header.php'); echo '<h2 style="color:red">我是首页</h2>'; //登录检测 if ((isset($_SESSION['user_id'])) && basename($_SERVER['PHP_SELF']) != 'logout.php') { echo '<a href="logout.php">退出</a>'; } else { echo '<a href="login.php">登录</a>'; } //导入底部 include('inc/footer.php');
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例 loggedin.php
<?php session_start(); //用户没有登录的验证 if(!isset($_SESSION['user_id'])) { require ('inc/function.php'); redirect_user(); } //如果已经登录 //设置页面标题 $page_title = '已经登录'; //导入头部 include ('inc/header.php'); //打印欢迎信息 echo <<< "WELCOME" <h2 style="color:red">登录成功</h2> <p>欢迎您:{$_SESSION['user_name']}</p> <p><a href="logout.php">退出</a></p> WELCOME; //导入底部 include ('inc/footer.php');
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例 login.php
<?php session_start(); if($_SERVER['REQUEST_METHOD'] == 'POST') { //验证用户的邮箱和密码 //跳转到指定的页面 //加载公共函数库 require ('inc/function.php'); //连接数据库 require ('inc/connect.php'); //验证登录 list($check,$data) = check_login($dbc, $_POST['email'],$_POST['password']); //验证通过 if ($check) { //设置SESSION $_SESSION['user_id'] = $data['user_id']; $_SESSION['user_name'] = $data['user_name']; //跳转页面 redirect_user('loggedin.php'); } else { $errors = $data; } //关闭连接 mysqli_close($dbc); } //加载 include('login_page.php');
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例 login_page.php
<?php /** * 用户登录页面 * 1.输出用户的错误信息 * 2.输出登录表单:当前的表单渲染 */ $page_title = '用户登录'; //导入头部 include('inc/header.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="email">邮箱:</label> <input type="email" name="email" id="email" value="<?php echo isset($_PSOST['email'])?$_POST['email']:' '?>"> </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> <?php //导入底部 include('inc/footer.php'); ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例 logout.php
<?php session_start(); //如何没有登录的验证 if(!isset($_SESSION['user_id'])) { require ('inc/function.php'); //跳转到默认首页 redirect_user(); } else { //删除cookies // $_setcookie('user_id','',time()-3600); // $_setcookie('user_name','',time()-3600); $_SESSION = []; session_destroy(); setcookie('PHPSESSID','',time()-3600); } //退出登录 $page_title = '退出成功'; //导入头部 include ('inc/header.php'); //打印欢迎信息 echo <<<"WELCOME" <h2 style="color:red">退出成功</h2> <p><a href="login.php">登录</a> </p> WELCOME; //导入底部 include('inc/footer.php');
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例 inc/connect.php
<?php define('DB_HOST','localhost'); define('DB_USER', 'root'); define('DB_PASS', 'root'); define('DB_NAME','php'); define('DB_CHAR','utf8'); $dbc = @mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME); if (mysqli_connect_errno($dbc)) { echo '连接失败'.mysqli_connect_error($dbc); } mysqli_select_db($dbc,DB_NAME); mysqli_set_charset($dbc,DB_CHAR);
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例 inc/function.php
<?php /** * 用户自定义跳转地址 * @param string $page */ function redirect_user($page = 'index.php') { //默认地址 $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); //去掉右侧的/,\ $_url - rtrim($url, '/\\'); //生成自定义跳转地址 $url .= '/'.$page; //跳转到指定的目标地址 header('location:'. $url); //退出当前的函数或脚本 exit(); } /** * @param $dbc * @param string $email * @param string $password * @return array */ function check_login($dbc,$email='',$password='') { //创建错误信息数组 $errors = []; //验证邮箱 if(empty($email)){ $errors[]= '邮箱地址不能为空'; } else { $e = mysqli_real_escape_string($dbc,trim($email)); } //验证密码 if(empty($password)){ $errors[]= '密码不能为空'; } else { $p = mysqli_real_escape_string($dbc,trim($password)); } //非空验证通过,即$error数组为空 if(empty($errors)) { //根据邮箱与密码来查询用户ID与用户名 $sql = "SELECT `user_id`,`user_name` FROM `user` WHERE `email`='$e' AND `password`=sha1('$p') "; //注意此处的FROM //执行查询 $res = mysqli_query($dbc,$sql); //查询成功应该返回唯一一条记录 if(mysqli_num_rows($res) ==1) { //将查询结果解析到数组中 $row = mysqli_fetch_array($res,MYSQLI_ASSOC); //返回查询结果 // print_r($row);die(); // print_r($row);exit(); return [true, $row]; //注意此处的return不要写错了! } else{ $errors[] = '邮箱或密码不正确,请重新输入'; } } return [false,$errors]; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例 inc/header.php
<!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>
运行实例 »
点击 "运行实例" 按钮查看在线实例