总结:
登录公共函数库
用户自定义跳转地址
默认url='http://.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])
如果有去掉url的斜线
跳转到指定目标地址
初始化错误信息数组
非空验证通过,即$error数组为空
现在越来越接近实战的项目了,感觉自己有点干部上老师的节奏了,一定要多付练习跟上老师。
实例
<?php //登录公共函数库 //用户自定义跳转地址@param string $page function redirect_user($page='index.php'); { //默认url='http://.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']) //如果有去掉url的斜线 $url = rtrim($url,'/\\'); //添加当前脚本的名称,默认:index.php $url .= '/'.$page; //跳转到指定目标地址 header('Location:'.$url); //退出昂前函数 exit(); } //验证用户登录 //@param $dbc //@param string $email //@param string $password function check_login($dbc,$email='',$password=''){ //初始化错误信息数组 $errors = []; //验证邮箱 if (empty($email)) { $error[] = '邮箱不能为空'; } else { //myaqli_rial_escape_string()转义字符串哩的特殊字符 myaqli_rial_escape_string($dbc,trim($email)); } if (empty($password)) { $error[] = '密码不能为空'; } else { myaqli_rial_escape_string($dbc,trim($password)); } //非空验证通过,即$error数组为空 if (empty($errors)) { //根据邮箱与密码来查询用户id和用户名 $aql = "SELECT SELECT `user_id`,`user_name` FROM `user` WHERE `email`='$e' AND `password`=sha1('$p') " ; //执行查询 $res = =mysqli_query($dbc,$sql); //查询成功应该返回一条记录 if (mysqli_num_rows($res) == 1) { //将查询结果解析到数组中 $row = mysqli_fetch_array($res, MYSQLI_ASSOC); //返回查询结果 // print_r($row);exit(); return [true, $row]; } else { //查询失败 $errors[] = '邮箱或密码不正确,请重新输入'; } } return [false, $errors]; } <?php $page_title = '首页'; include ('inc/header.php')//导入header.php echo '<h2 style="color:red">我是首页</h2>'; if ((isset($_COOKIE['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'); <?php if (!isset($_COOKIE['user_id'])) { require ('inc/function.php'); //跳转到默认首页 redirect_user(); } //设置页面标题 $page_title = '已经登录'; include('inc/feader.php'); //打印欢迎信息,并提供退出按钮 echo <<<"HI" <h2 style="color:red">登录成功</h2> <p>欢迎您:[$_COOKIE['user_name']]</p> <p><a href="logout.php">退出</a></p> HI; //底部 include('inc/footer.php'); <?php 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) { //设置cookies setcookie('use_id',$data['user_id']); setcookie('use_name',$data['user_name']); //跳转页面 redirect_user('loggedin.php'); } else{ //验证失败 $errors = $data; } //关闭数据库链接 mysqli_close($dbc); } //加载 include('login_page.php');require('inc/function.php'); <?php //登录页面并报告错误 //设置当前页面打的标题 //在login.php中调用 $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" mathod="post"> <p> <lable form="email">邮箱:</lable> <!--使用粘性表单技术在文本框中显示用户之前输入的内容,提升用户体验--> <input type="email" name="email" id="email" value="<?php echo isset($_POST['email'])?$_POST['email']:'' ?"> </p> <p> <lable form="password">邮箱:</lable> <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> <?php include('inc/footer.php); //加载底部 ?> <?php if (!isset($_COOKIE['user_id'])) { require('inc/function.php'); //跳转到默认首页 redirect_user(); } else { //删除cookies setcookie('user_id', '' ,time()-1); setcookie('user_name', '' ,time()-1); } //设置标题 $page_title = '已登录'; include('inc/header.php'); //打印退出信息,并提供登录功能 echo <<<"HI" <h2 style="color:red">退出成功</h2> <p><a href="login.php">登录</a></p> HI; include('inc/footer.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); //连接失败一定返回错误编码号,可以根据判断,也可以用$db是否为false进行判断 if (mysqli_connect_errno($dbc)) { echo '连接失败' .mysqli_connect_errno($dbc); } mysqli_select_db($dbc,DB_NAME);//选择操作的数据库 mysqli_set_charset($dbc,DB_CHAR);//设置客户端默认的字符编码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo isset($page_title)? $page_title:'默认标题' ?> </title> </head> <body> <h2>公共头部</h2> <h2>公共脚步代码</h2> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php //登录公共函数库 //用户自定义跳转地址@param string $page function redirect_user($page='index.php'); { //默认url='http://.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']) //如果有去掉url的斜线 $url = rtrim($url,'/\\'); //添加当前脚本的名称,默认:index.php $url .= '/'.$page; //跳转到指定目标地址 header('Location:'.$url); //退出昂前函数 exit(); } //验证用户登录 //@param $dbc //@param string $email //@param string $password function check_login($dbc,$email='',$password=''){ //初始化错误信息数组 $errors = []; //验证邮箱 if (empty($email)) { $error[] = '邮箱不能为空'; } else { //myaqli_rial_escape_string()转义字符串哩的特殊字符 myaqli_rial_escape_string($dbc,trim($email)); } if (empty($password)) { $error[] = '密码不能为空'; } else { myaqli_rial_escape_string($dbc,trim($password)); } //非空验证通过,即$error数组为空 if (empty($errors)) { //根据邮箱与密码来查询用户id和用户名 $aql = "SELECT SELECT `user_id`,`user_name` FROM `user` WHERE `email`='$e' AND `password`=sha1('$p') " ; //执行查询 $res = =mysqli_query($dbc,$sql); //查询成功应该返回一条记录 if (mysqli_num_rows($res) == 1) { //将查询结果解析到数组中 $row = mysqli_fetch_array($res, MYSQLI_ASSOC); //返回查询结果 // print_r($row);exit(); return [true, $row]; } else { //查询失败 $errors[] = '邮箱或密码不正确,请重新输入'; } } return [false, $errors]; } <?php $page_title = '首页'; include ('inc/header.php')//导入header.php echo '<h2 style="color:red">我是首页</h2>'; if ((isset($_COOKIE['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'); <?php if (!isset($_COOKIE['user_id'])) { require ('inc/function.php'); //跳转到默认首页 redirect_user(); } //设置页面标题 $page_title = '已经登录'; include('inc/feader.php'); //打印欢迎信息,并提供退出按钮 echo <<<"HI" <h2 style="color:red">登录成功</h2> <p>欢迎您:[$_COOKIE['user_name']]</p> <p><a href="logout.php">退出</a></p> HI; //底部 include('inc/footer.php'); <?php 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) { //设置cookies setcookie('use_id',$data['user_id']); setcookie('use_name',$data['user_name']); //跳转页面 redirect_user('loggedin.php'); } else{ //验证失败 $errors = $data; } //关闭数据库链接 mysqli_close($dbc); } //加载 include('login_page.php');require('inc/function.php'); <?php //登录页面并报告错误 //设置当前页面打的标题 //在login.php中调用 $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" mathod="post"> <p> <lable form="email">邮箱:</lable> <!--使用粘性表单技术在文本框中显示用户之前输入的内容,提升用户体验--> <input type="email" name="email" id="email" value="<?php echo isset($_POST['email'])?$_POST['email']:'' ?"> </p> <p> <lable form="password">邮箱:</lable> <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> <?php include('inc/footer.php); //加载底部 ?> <?php if (!isset($_COOKIE['user_id'])) { require('inc/function.php'); //跳转到默认首页 redirect_user(); } else { //删除cookies setcookie('user_id', '' ,time()-1); setcookie('user_name', '' ,time()-1); } //设置标题 $page_title = '已登录'; include('inc/header.php'); //打印退出信息,并提供登录功能 echo <<<"HI" <h2 style="color:red">退出成功</h2> <p><a href="login.php">登录</a></p> HI; include('inc/footer.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); //连接失败一定返回错误编码号,可以根据判断,也可以用$db是否为false进行判断 if (mysqli_connect_errno($dbc)) { echo '连接失败' .mysqli_connect_errno($dbc); } mysqli_select_db($dbc,DB_NAME);//选择操作的数据库 mysqli_set_charset($dbc,DB_CHAR);//设置客户端默认的字符编码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo isset($page_title)? $page_title:'默认标题' ?> </title> </head> <body> <h2>公共头部</h2> <h2>公共脚步代码</h2> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?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); //连接失败一定会返回错误编号,可以根据编号判断,也可用 $db是否为false进行判断 if (mysqli_connect_errno($dbc)) { echo '连接失败'.mysqli_connect_error($dbc); } mysqli_select_db($dbc, DB_NAME); //选择要操作的数据库 mysqli_set_charset($dbc, DB_CHAR); //设置客户端默认字符编码集 <h3>我是公共底部的代码</h3> </body> </html> <?php //登录公共函数库 /** * 用户自定义跳转地址 * @param string $page */ function redirect_user($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(); } /** * 验证用户登录 * @param $dbc * @param string $email * @param string $password */ function check_login($dbc, $email='', $password='') { //初始化错误信息数组 $errors = []; //验证邮箱 if (empty($email)) { $errors[] = '邮箱地址不能为空'; } else { //mysqli_real_escape_string():转义字符串的特殊字符 $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') "; //执行查询 $res = mysqli_query($dbc, $sql); //查询成功应该返回唯一一条记录 if (mysqli_num_rows($res) == 1) { //将查询结果解析到数组中 $row = mysqli_fetch_array($res, MYSQLI_ASSOC); //返回查询结果 // print_r($row);exit(); return [true, $row]; } else { //查询失败 $errors[] = '邮箱或密码不正确,请重新输入'; } } return [false, $errors]; } <!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> <?php session_start(); //开启会话 $page_title = '首页'; include ('inc/header.php'); echo '<h2 style="color:red">我是首页</h2>'; //if ((isset($_COOKIE['user_id'])) && basename($_SERVER['PHP_SELF']) != 'logout.php') { 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'); <?php //开启会话 session_start(); //if (!isset($_COOKIE['user_id'])) { 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'); <?php //开启会话 session_start(); if ($_SERVER['REQUEST_METHOD'] == 'POST') { //加载公共函数库 require ('inc/function.php'); //连接数据库 require ('inc/connect.php'); //验证登录 //$check=true/false; $data=['user_id'=>$user_id, 'user_name'=>$user_name]; list($check, $data) = check_login($dbc, $_POST['email'], $_POST['password']); //验证通过 if ($check) { //设置cookies // setcookie('user_id', $data['user_id']); // setcookie('user_name', $data['user_name']); //设置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'); <?php /** * 登录页面并报告错误 * 设置当前页面的标题 * 在login.php中调用 */ $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($_POST['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'); //加载底部 ?> <?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');
运行实例 »
点击 "运行实例" 按钮查看在线实例