无知的我接触到了PHP,学习了PHP并尝试做了第一个比较合适的PHP项目。同时我正在不断优化自己写笔记的方法
目的是 为了解决第一次学习PHP的人能够快速通过可入手的完整的项目来串联知识点构建良好的体系以及提升运用这些知识的能力
目录
PHPbbs系统—后台
1 通用配置
1.1 静态资源
1.2 数据库
1.2.1 数据库配置信息
在sfkbbs>inc>创建config,inc.php:
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '123456');
define('DB_DATABASE', 'sfkbbs');
define('DB_PORT', '3306');
header('Content-type:text/html;charset=utf-8');
1.2.2 数据库工具方法
在sfkbbs>inc>创建mysql.inc.php:
<?php
//连接数据库,如果连接错误,则输出错误信息
//返回连接钥匙
function connect($host=DB_HOST,$user=DB_USER,$password=DB_PASSWORD,$database=DB_DATABASE,$post=DB_PORT){
$link = @mysqli_connect($host,$user,$password,$database,$post);
if(mysqli_connect_errno()){
exit(mysqli_connect_error());
}
mysqli_set_charset($link, 'utf8');
return $link;
}
//执行一条SQL语句
//返回结果集
function execute($link,$query){
$result = mysqli_query($link, $query);
if(mysqli_errno($link)){
exit(mysqli_error($link));
}
return $result;
}
//一次性执行多条sql语句,如果有错误则输出错误信息
//传入:连接钥匙,sql语句数组;引用地传入错误信息
//返回查询结果集
function execute_multi($link,$arr_sqls,&$error){
$sqls=implode(';',$arr_sqls).';';
if(mysqli_multi_query($link,$sqls)){
$data=array();
$i=0;//计数
do {
if($result=mysqli_store_result($link)){
$data[$i]=mysqli_fetch_all($result);
mysqli_free_result($result);
}else{
$data[$i]=null;
}
$i++;
if(!mysqli_more_results($link)) break;
}while (mysqli_next_result($link));
if($i==count($arr_sqls)){
return $data;
}else{
$error="sql语句执行失败:<br /> 数组下标为{$i}的语句:{$arr_sqls[$i]}执行错误<br /> 错误原因:".mysqli_error($link);
return false;
}
}else{
$error='执行失败!请检查首条语句是否正确!<br />可能的错误原因:'.mysqli_error($link);
return false;
}
}
//获取记录数
function num($link,$sql_count){
$result=execute($link,$sql_count);
$count=mysqli_fetch_row($result);
return $count[0];
}
//结果集语句数量
function execute_num($link,$sql_count){
$execute_result = execute($link, $sql_count);
$execute_num = mysqli_fetch_row($execute_result);
return $execute_num[0];
}
//转义操作!!!!!!!!!!递归函数的应用的一个实例!!!!!!!
function escapes($link, $data){
if (is_string($data)){
$data = mysqli_real_escape_string($link, $data);
return $data;
}
if (is_array($data)){
foreach ($data as $key=>$val){
$data[$key] = escapes($link, $val);
}
}
return $data;
}
//关闭数据库
function close($link){
mysqli_close($link);
}
1.3 二次确认页
在sfkbbs>inc>创建tool.php
<?php
function skip($url,$class,$message){
//!!!注意定界符里面不能解析php标签
//!!!!注意如何在meta里面设置自动跳转语法
$html = <<<A
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="3; URL={$url}"/>
<title>正在跳转</title>
<link rel="stylesheet" type="text/css" href="style/remind.css" />
</head>
<body>
<div class="notice"><span class="pic {$class}"></span>{$message}<a href="{$url}">3秒后自动跳转</div>
</body>
</html>
A;
echo $html;
exit;//不写括号也可以?对
}
?>
2 父板块
2.1 页面展示
2.2 添加
2.3 删除
2.4 修改
3 子板块
3.1 页面展示
在sfkbbs>admin>创建son_module.php文件
任务:显示子版块数据中的所有内容
解决:遍历查询结果集
<?php
include_once '../inc/config.inc.php';//此前不能添加任何信息?
include_once '../inc/mysql.inc.php';
$link = connect();
$template['title'] = '父板块';
?>
<?php include 'inc/header.inc.php';?>
<div id="main">
<div class="title">功能说明</div>
<table class="list">
<tr>
<th>排序</th>
<th>版块名称</th>
<th>所属父版块</th>
<th>版主</th>
<th>操作</th>
</tr>
<?php
$query = "select * from sfk_son_module";
$result = execute($link, $query);
while ($data = mysqli_fetch_assoc($result)){
$html = <<<A
<tr>
<td><input class="sort" type="text" name="sort" /></td>
<td>{$data["module_name"]}[id:{$data["id"]}]</td>
<td>
<a href="#">[访问]</a>
<a href="#">[编辑]</a>
<a href="#">[删除]</a>
</td>
</tr>
A;
echo $html;
}
?>
</table>
</div>
<?php include 'inc/footer.inc.php';?>
3.2 添加
1 添加按钮
2 添加验证
3.3 删除
1 删除按钮
在sfkbbs>admin>创建son_module_delete.php
-需求分析以及实现:
1.点击删除按钮,跳转到二次确认页面。
2.在二次确认页面,
-
配置二次确认页面:利用地址传参:1.传入 m e s s a g e u r l (提示信息) 2. message_url(提示信息) 2. messageurl(提示信息)2.return_url(返回的地址)
-
点击确认删除按钮:跳转到后台的子版块删除页面,根据传入的id值,执行数据库删除操作。
- 如何跳转到后台的子版块删除页面:利用地址传参
- 如何传递id值:利用地址传参
-
点击取消按钮:不删除。
-
点击最终结果:1.自动跳转回子版块展示页面 (需要重新查询数据库,而更新页面)
<?php
include_once '../inc/config.inc.php';//此前不能添加任何信息?
include_once '../inc/mysql.inc.php';
$link = connect();
$template['title'] = '父板块';
?>
<?php include 'inc/header.inc.php';?>
<div id="main">
<div class="title">功能说明</div>
<table class="list">
<tr>
<th>排序</th>
<th>版块名称</th>
<th>所属父版块</th>
<th>版主</th>
<th>操作</th>
</tr>
<?php
$query = "select * from sfk_son_module";
$result = execute($link, $query);
while ($data = mysqli_fetch_assoc($result)){
//通过传递url来让目的页面可以跳转传递过来的指定页面
//注意!!!为了返回url参数注入,不能直接传递【url+url参数】,需要以下两种办法传递
$delete_url = urlencode("son_module_delete.php?id={$data['id']}");//如果这边用字符串形式设置url参数,要注意双引号入住
$return_url = urlencode($_SERVER['REQUEST_URI']);//!!!获取当前页面的url!!!
$message_url = urldecode("{$data["module_name"]}");
$confirm_ulr = "confirm.php?delete_url={$delete_url}&return_url={$return_url}&message_url={$message_url}";
//!!!!记住这种定界符写法!!!!
$html = <<<A
<tr>
<td><input class="sort" type="text" name="sort" /></td>
<td>{$data["module_name"]}[id:{$data["id"]}]</td>
<td>
<a href="#">[访问]</a>
<a href="son_module_update.php?id={$data['id']}">[编辑]</a>
<a href="$confirm_ulr">[删除]</a>
</td>
</tr>
A;
echo $html;
}
?>
</table>
</div>
<?php include 'inc/footer.inc.php';?>
2 父板块删除补充
在sfkbbs>admin>father_module_delete.php 添加下面配置
**-需求:**当父板块存在子板块时,作删除操作,需要二次确认。
<?php
include_once '../inc/config.inc.php';
include_once '../inc/mysql.inc.php';
include_once '../inc/tool.php';
$url = "father_module.php";
if(!isset($_GET["id"]) || !is_numeric($_GET["id"])){//$_GET["id"]其实就是一个以字符串为索引的数组
skip($url, "error", "id参数不存在!");
}//!!!注意!!!在此处如果不设置停止,其会继续执行下面语句,所以需要停止,停止方式有多种看详情!!!!!
$link = connect();
/**
* 若父板块存在子板块(若子板块存在父板块)
* 则当删除该父板块时,需要二次确认。
*/
$query = "select * from sfk_son_module where father_module_id = {$_GET['id']}";
$result = execute($link, $query);
if (mysqli_num_rows($result)){
skip($url, 'error', '该父板块存在子板块!需要先删除对应子版块!');
}
$query = "delete from sfk_father_module where id = {$_GET['id']}";
execute($link, $query);
if (mysqli_affected_rows($link) == 1){
skip($url, "ok", "恭喜删除成功!");
}else {
skip($url, "error", "删除失败!");
}
//!!!!url地址传递参数,传递的其实是字符串,在其显示上省略了双引号
3.4 修改
1 修改按钮
在skfbbs>admin>创建son_module_update.php
<?php $template['title'] = '子版块-修改'?>
<?php include '../inc/config.inc.php';?>
<?php include '../inc/mysql.inc.php';?>
<?php include '../inc/tool.php'?>
<?php
//1,对传入的id值验证
/*
* 问题:在地址栏输入错误的id
* 跳转回页面
*/
if (!isset($_GET['id']) || !is_numeric($_GET['id'])){
skip("son_module_update.php", "error", "id参数错误");
}
/*
* 问题:对编辑的板块,其信息不存在
* 跳转回页面
*/
$link = connect();
$query = "select * from sfk_son_module where id='{$_GET['id']}'" ;//注意这里需要用get而不是post,因为是通过url地址传递参数
$result = execute($link, $query);
if(!mysqli_num_rows($result)){
skip("son_module_update.php", "error", "该板块信息不存在");
}
//对传递的要修改的内容验证。
/*
* 设置当按下提交后,才执行数据库修改操作
*/
$data = mysqli_fetch_assoc($result);//将结果集转化为数组的形式
if(isset($_POST['submit'])){
/*
* 对插入语句参数,处理
*/
$check_flag = 'update';
include 'inc/check_son_module.inc.php';
/*
* 执行修改语句
*/
$query = "update sfk_son_module set
father_module_id ='{$_POST['father_module_id']}',
module_name = '{$_POST['module_name']}',
info='{$_POST['info']}',
sort={$_POST['sort']} ,
member_id={$_POST['member_id']}
where id ={$_GET['id']} ";
$result = execute($link, $query);
/*
* 判断修改是否成功
*/
if(mysqli_affected_rows($link) == 1){//传入参数是数据库钥匙!!!!
skip('son_module.php', 'ok', '修改成功!');
}else {
skip('son_module.php', "error", '修改失败!');
}
}
?>
<?php include 'inc/header.inc.php';?>
<div id="main">
<div class="title" style="margin-bottom: 20px;">功能说明</div>
<form method="post">
<table class="au">
<tr>
<td>所属父板块</td>
<td>
<select name="father_module_id">
<option value = "0">======请选择一个父版块======</option>
<?php
$query = "select * from sfk_father_module";
$result = execute($link, $query);
while ($data_father = mysqli_fetch_assoc($result)){
if($data['father_module_id'] == $data_father['id']){
//让其被选中
echo "<option selected='selected' value = '{$data_father['id']}'>{$data_father['module_name']}</option>";
}else {
echo "<option value = '{$data_father['id']}'>{$data_father['module_name']}</option>";
//此处不能用双引号
}
}
?>
</select>
</td>
<td>
不得选择空值
</td>
</tr>
<tr>
<td>版块名称</td>
<td><input name="module_name" type="text" value="<?php echo $data['module_name']?>" /></td>
<td>
版块名称不得为空,最大不得超过66个字符
</td>
</tr>
<tr>
<td>版块简介</td>
<td>
<textarea name="info"><?php echo $data['info']?></textarea>
</td>
<td>
简介不得多于255个字符
</td>
</tr>
<tr>
<td>版主</td>
<td>
<select name="member_id">
<option value="0">======请选择一个会员作为版主======</option>
</select>
</td>
<td>
你可以在这边选一个会员作为版主
</td>
</tr>
<tr>
<td>排序</td>
<td><input name="sort" value="<?php echo $data['sort']?>" type="text" /></td>
<td>
填写一个数字即可
</td>
</tr>
</table>
<input style="margin-top: 20px; cursor:pointer" class="btn" type="submit" name="submit" value="修改" />
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
2 修改验证
在admin>inc>check_son_module.inc.php
<?php
/*
* 父板块下拉列表——判断是否选择——根据传参是否为空
*/
if(!is_numeric($_POST['father_module_id'])){
skip('son_module_add.php', 'error', '所属父板块不能为空!');
}//注意!!!!!!url的参数都是字符串!!没有什么特殊符号之分
// if(!is_string($_POST['father_module'])){
// skip('father_module_add.php', 'error', '不能输入特殊符号');
// }
/*
* 父板块下拉列表——父板块是否存在——根据传参的id
* 细节:!!!问题处理:对sql注入的毛病!!!!
*/
$link = connect();
$query="select * from sfk_father_module where id={$_POST['father_module_id']}";
$result = execute($link, $query);
if(mysqli_num_rows($result)==0){
skip('son_module_add.php','error','所属父版块不存在!');
}
/*
* 版块名称——判断是否为空;是否大于66个字符数量;是否已经存在——根据传入名字的参数
* 例外:对修改操作,联合判断两个:id、板块名称
*/
if (empty($_POST['module_name'])){
skip('son_module_add.php', 'error', '子版块名称不能为空!');
}
if(mb_strlen($_POST['module_name']) > 66){
skip('son_module_add.php', 'error', '输入的字符数量大于了66!');
}
$_POST = escapes($link, $_POST);
switch ($check_flag){
case 'add':
$query="select * from sfk_son_module where module_name='{$_POST['module_name']}'";
break;
case 'update':
$query="select * from sfk_son_module where module_name='{$_POST['module_name']}' and id!={$_GET['id']}";//!!!!巧妙:!!!此处可以写id也可以写sort去判断!!!!
break;
default:
skip('father_module_add.php','error','$check_flag参数错误!');
}
$result = execute($link, $query);
if (mysqli_num_rows($result)){
skip('son_module_add.php', 'error', '该子板块已经存在!');
}
/*
* 判断简介和排序
*/
if(mb_strlen($_POST['info'])>255){
skip('son_module_add.php','error','子版块简介不得多于255个字符!');
}
if(!is_numeric($_POST['sort'])){
skip('son_module_add.php','error','排序只能是数字!');
}
3 侧边栏
在sfkbbs>admin>inc>header.inc.php 添加配置
-任务:当编辑时,添加编辑按钮
<?php
?>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<!-- !!!!此处利用天然条件判断!!!变量得以发挥其作用 -->
<title><?php echo $template['title']?></title>
<meta name="keywords" content="后台界面" />
<meta name="description" content="后台界面" />
<link rel="stylesheet" type="text/css" href="style/public.css" />
</head>
<body>
<div id="top">
<div class="logo">
管理中心
</div>
<ul class="nav">
<li><a href="http://www.sifangku.com" target="_blank">私房库</a></li>
<li><a href="http://www.sifangku.com" target="_blank">私房库</a></li>
</ul>
<div class="login_info">
<a href="#" style="color:#fff;">网站首页</a> |
管理员: admin <a href="#">[注销]</a>
</div>
</div>
<div id="sidebar">
<ul>
<li>
<div class="small_title">系统</div>
<ul class="child">
<li><a class="current" href="#">系统信息</a></li>
<li><a href="#">管理员</a></li>
<li><a href="#">添加管理员</a></li>
<li><a href="#">站点设置</a></li>
</ul>
</li>
<li><!-- class="current" -->
<div class="small_title">内容管理</div>
<ul class="child"><!--SCRIPT_NAME:script_name--> <!-- 先输出在html页面 -->
<li><a class="<?php if(basename($_SERVER['SCRIPT_NAME']) == "father_module.php"){echo 'current';}?>" href="father_module.php">父板块列表</a></li>
<li><a class="<?php if(basename($_SERVER['SCRIPT_NAME'])== "father_module_add.php"){echo 'current';}?>" href="father_module_add.php">添加父板块</a></li>
<?php
if(basename($_SERVER['SCRIPT_NAME']) == "father_module_update.php"){
echo '<li><a class="current">编辑父板块</a></li>';
}
?>
<li><a class="<?php if(basename($_SERVER['SCRIPT_NAME'])== "son_module.php"){echo 'current';}?>" href="son_module.php">子板块列表</a></li>
<li><a class="<?php if(basename($_SERVER['SCRIPT_NAME'])== "son_module_add.php"){echo 'current';}?>" href="son_module_add.php">添加子板块</a></li>
<?php
if(basename($_SERVER['SCRIPT_NAME']) == "son_module_update.php"){
echo '<li><a class="current">编辑子板块</a></li>';
}
?>
<li><a href="#">帖子管理</a></li>
</ul>
</li>
<li>
<div class="small_title">用户管理</div>
<ul class="child">
<li><a href="#">用户列表</a></li>
</ul>
</li>
</ul>
</div>
- 报错:
Unknown column 'asdasdsdfcasd' in 'field list'
- 翻译:不知道这个字段是什么。原因:可能是sql语句的字段名类型写错了
4 父板块排序
4.1 获取排序
在admin>father_module.php 添加配置
<?php
include_once '../inc/config.inc.php';//此前不能添加任何信息?
include_once '../inc/mysql.inc.php';
$link = connect();
$template['title'] = '父板块';
?>
<?php include 'inc/header.inc.php';?>
<div id="main">
<div class="title">功能说明</div>
<form method="post">
<table class="list">
<tr>
<th>排序</th>
<th>版块名称</th>
<th>操作</th>
</tr>
<?php
$query = "select * from sfk_father_module";
$result = execute($link, $query);
while ($data = mysqli_fetch_assoc($result)){
//通过传递url来让目的页面可以跳转传递过来的指定页面
//注意!!!为了返回url参数注入,不能直接传递【url+url参数】,需要以下两种办法传递
$delete_url = urlencode("father_module_delete.php?id={$data['id']}");//如果这边用字符串形式设置url参数,要注意双引号入住
$return_url = urlencode($_SERVER['REQUEST_URI']);//!!!获取当前页面的url!!!
$message_url = urldecode("{$data["module_name"]}");
$confirm_ulr = "confirm.php?delete_url={$delete_url}&return_url={$return_url}&message_url={$message_url}";
//!!!!记住这种定界符写法!!!!
$html = <<<A
<tr>
<td><input class="sort" type="text" name="sort[{$data['id']}]" value="{$data['sort']}" /></td>
<td>{$data["module_name"]}[id:{$data["id"]}]</td>
<td><a href="#">[访问]</a> <a href="father_module_update.php?id={$data['id']}">[编辑]</a> <a href="$confirm_ulr">[删除]</a></td>
</tr>
A;
echo $html;
}
?>
</table>
<input style="margin-top:20px;cursor:pointer;" class="btn" type="submit" name="submit" value="排序" />
</form>
</div>
<?php include 'inc/footer.inc.php';?>
4.2 修改排序
-问题?:
- 在这种情况下,同名参数如何包含多个参数值?
- 如何一次性执行多个数据库操作?
**-细节:**1.引入方法。2.验证传入sql
语句的参数 (排序只能是数字,否则sql语句错误)。3.注意if_else语句
<?php
include_once '../inc/config.inc.php';//此前不能添加任何信息?
include_once '../inc/mysql.inc.php';
include_once '../inc/tool.php';
$link = connect();
if(isset($_POST['submit'])){
foreach ($_POST['sort'] as $key=>$value){
/**
* 若传入的id 和 sort不是数字
* 则报错
*/
if(!is_numeric($value) || !is_numeric($key)){
skip("father_module.php", "error", "排序只能是数字");
}//不需要else,因为前面错误会跳转
$query[] = "update sfk_father_module set sort={$value} where id={$key}";
// echo $query[];//不能直接输出数组
}
if(execute_multi($link, $query, $error)){
skip("father_module.php", "ok", "排序修改成功!");
}else {//要写else, 否则无论如何 都会执行跳转。
skip("father_module.php", "error", $error);
}
}
$template['title'] = '父板块';
?>
<?php include 'inc/header.inc.php';?>
<div id="main">
<div class="title">功能说明</div>
<form method="post">
<table class="list">
<tr>
<th>排序</th>
<th>版块名称</th>
<th>操作</th>
</tr>
<?php
$query = "select * from sfk_father_module";
$result = execute($link, $query);
while ($data = mysqli_fetch_assoc($result)){
//通过传递url来让目的页面可以跳转传递过来的指定页面
//注意!!!为了返回url参数注入,不能直接传递【url+url参数】,需要以下两种办法传递
$delete_url = urlencode("father_module_delete.php?id={$data['id']}");//如果这边用字符串形式设置url参数,要注意双引号入住
$return_url = urlencode($_SERVER['REQUEST_URI']);//!!!获取当前页面的url!!!
$message_url = urldecode("{$data["module_name"]}");
$confirm_ulr = "confirm.php?delete_url={$delete_url}&return_url={$return_url}&message_url={$message_url}";
//!!!!记住这种定界符写法!!!!
$html = <<<A
<tr>
<td><input class="sort" type="text" name="sort[{$data['id']}]" value="{$data['sort']}" /></td>
<td>{$data["module_name"]}[id:{$data["id"]}]</td>
<td><a href="#">[访问]</a> <a href="father_module_update.php?id={$data['id']}">[编辑]</a> <a href="$confirm_ulr">[删除]</a></td>
</tr>
A;
echo $html;
}
?>
</table>
<input style="margin-top:20px;cursor:pointer;" class="btn" type="submit" name="submit" value="排序" />
</form>
</div>
<?php include 'inc/footer.inc.php';?>
5 子版块排序
在sfkbbs>admin>son_module.php
<?php
include_once '../inc/config.inc.php';//此前不能添加任何信息?
include_once '../inc/mysql.inc.php';
include_once '../inc/tool.php';
$link = connect();
$template['title'] = '子板块展示';//页面标题
if(isset($_POST['submit'])){
foreach ($_POST['sort'] as $key=>$val){
if(!is_numeric($val) || !is_numeric($key)){
skip('son_module.php','error','排序参数错误!');
}
$query[]="update sfk_son_module set sort={$val} where id={$key}";
}
if(execute_multi($link,$query,$error)){
skip('son_module.php','ok','排序修改成功!');
}else{
skip('son_module.php','error',$error);
}
}
?>
<?php include 'inc/header.inc.php';?>
<div id="main">
<div class="title">功能说明</div>
<form method="post">
<table class="list">
<tr>
<th>排序</th>
<th>版块名称</th>
<th>所属父版块</th>
<th>版主</th>
<th>操作</th>
</tr>
<?php
$query="select ssm.id,ssm.sort,ssm.module_name,sfm.module_name father_module_name,ssm.member_id from sfk_son_module ssm,sfk_father_module sfm where ssm.father_module_id=sfm.id order by sfm.id";
$result = execute($link, $query);
while ($data = mysqli_fetch_assoc($result)){
//通过传递url来让目的页面可以跳转传递过来的指定页面
//注意!!!为了返回url参数注入,不能直接传递【url+url参数】,需要以下两种办法传递
$delete_url = urlencode("son_module_delete.php?id={$data['id']}");//如果这边用字符串形式设置url参数,要注意双引号入住
$return_url = urlencode($_SERVER['REQUEST_URI']);//!!!获取当前页面的url!!!
$message_url = urldecode("{$data["module_name"]}");
$confirm_ulr = "confirm.php?delete_url={$delete_url}&return_url={$return_url}&message_url={$message_url}";
//!!!!记住这种定界符写法!!!!
$html = <<<A
<tr>
<td><input class="sort" type="text" name="sort[{$data['id']}]" value="{$data['sort']}" /></td>
<td>{$data["module_name"]}[id:{$data["id"]}]</td>
<td>
<a href="#">[访问]</a>
<a href="son_module_update.php?id={$data['id']}">[编辑]</a>
<a href="$confirm_ulr">[删除]</a>
</td>
</tr>
A;
echo $html;
}
?>
</table>
<input style="margin-top:20px;cursor:pointer;" class="btn" type="submit" name="submit" value="排序" />
</form>
</div>
<?php include 'inc/footer.inc.php';?>
修改样式失效问题
问题描述:修改样式后,在页面没有生效:
- 需要清空浏览器缓存
- 页面编码格式不一致导致
PHPbbs系统—用户
6 用户注册
6.1 创建表
细节:1.添加AI注释 2.存储引擎为MyISAM(下方应该改)
6.2 创建样式文件夹
在sfkbbs>创建style文件夹
步骤:放入相关的样式文件以及图片
6.3 创建注册页
在sfkbbs>创建register.php
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link rel="stylesheet" type="text/css" href="style/public.css" />
<link rel="stylesheet" type="text/css" href="style/register.css" />
</head>
<body>
<div class="header_wrap">
<div id="header" class="auto">
<div class="logo">sifangku</div>
<div class="nav">
<a class="hover">首页</a>
</div>
<div class="serarch">
<form>
<input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
<input class="submit" type="submit" name="submit" value="" />
</form>
</div>
<div class="login">
<a>登录</a>
<a>注册</a>
</div>
</div>
</div>
<div style="margin-top:55px;"></div>
<div id="register" class="auto">
<h2>欢迎注册成为 私房库会员</h2>
<form method="post">
<label>用户名:<input type="text" name="name" /><span>*用户名不得为空,并且长度不得超过32个字符</span></label>
<label>密码:<input type="password" name="pw" /><span>*密码不得少于6位</span></label>
<label>确认密码:<input type="password" name="confirm_pw" /><span>*请输入与上面一致</span></label>
<label>验证码:<input name="vcode" name="vocode" type="text" /><span>*请输入下方验证码</span></label>
<img class="vcode" src="style/show_code.php.jpg" />
<div style="clear:both;"></div>
<input class="btn" name="submit" type="submit" value="确定注册" />
</form>
</div>
<div id="footer" class="auto">
<div class="bottom">
<a>私房库</a>
</div>
<div class="copyright">Powered by sifangku ©2021 sifangku.com</div>
</div>
</body>
</html>
6.4 配置注册页面
//主要步骤:
- 传入参数验证
- 执行数据库插入操作
- 跳转而结果提示
//可能出现问题:样式加载错误。
//解决:浏览器ctrl + f5:清空缓存。
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
if(isset($_POST['submit'])){
include 'inc/check_register.inc.php';
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link rel="stylesheet" type="text/css" href="style/public.css" />
<link rel="stylesheet" type="text/css" href="style/register.css" />
</head>
<body>
<div class="header_wrap">
<div id="header" class="auto">
<div class="logo">sifangku</div>
<div class="nav">
<a class="hover">首页</a>
</div>
<div class="serarch">
<form>
<input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
<input class="submit" type="submit" name="submit" value="" />
</form>
</div>
<div class="login">
<a>登录</a>
<a>注册</a>
</div>
</div>
</div>
<div style="margin-top:55px;"></div>
<div id="register" class="auto">
<h2>欢迎注册成为 私房库会员</h2>
<form method="post">
<label>用户名:<input type="text" name="name" /><span>*用户名不得为空,并且长度不得超过32个字符</span></label>
<label>密码:<input type="password" name="pw" /><span>*密码不得少于6位</span></label>
<label>确认密码:<input type="password" name="confirm_pw" /><span>*请输入与上面一致</span></label>
<label>验证码:<input name="vcode" name="vocode" type="text" /><span>*请输入下方验证码</span></label>
<img class="vcode" src="style/show_code.php.jpg" />
<div style="clear:both;"></div>
<input class="btn" name="submit" type="submit" value="确定注册" />
</form>
</div>
<div id="footer" class="auto">
<div class="bottom">
<a>私房库</a>
</div>
<div class="copyright">Powered by sifangku ©2021 sifangku.com</div>
</div>
</body>
</html>
6.5 创建验证
在sfkbbs>inc>check_register.inc.php配置
<?php
if(empty($_POST['name'])){
skip("register.php", "error", "用户名不得为空!");
}
if(mb_strlen($_POST['name']) > 32){
skip("register.php", "error", "用户名不得超过32位!");
}
if(mb_strlen($_POST['pw'] < 6)){
skip("register.php", "error", "密码不得少于6位!");
}
if($_POST['pw'] != $_POST['confirm_pw']){
skip("register.php", "error", "两次输入的密码不一致!");
}
?>
细节:
- / 代表工程根目录
if(empty($_POST['name'])){
skip("/register.php", "error", "用户名不得为空!");
}
- 代表当前项目根目录
if(empty($_POST['name'])){
skip("register.php", "error", "用户名不得为空!");
}
6.6 存储注册
在sfkbbs>register配置
//补充:
- md5()函数:数据库存储加密密码。
- password_hash: 具有同样的功能,更先进。
- 可能遇到的问题1:数据库插入不成功:
Field 'last_time' doesn't have a default value。
- 解决1:在数据库,赋值剩余字段为NULL。
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
if(isset($_POST['submit'])){
$link=connect();
include 'inc/check_register.inc.php';
$_POST = escapes($link, $_POST);
$query="insert into sfk_member(name,pw,register_time) values('{$_POST['name']}',md5('{$_POST['pw']}'),now())";
execute($link,$query);
if(mysqli_affected_rows($link)==1){
skip('index.php','ok','注册成功!');
}else{
skip('register.php','eror','注册失败,请重试!');
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link rel="stylesheet" type="text/css" href="style/public.css" />
<link rel="stylesheet" type="text/css" href="style/register.css" />
</head>
<body>
<div class="header_wrap">
<div id="header" class="auto">
<div class="logo">sifangku</div>
<div class="nav">
<a class="hover">首页</a>
</div>
<div class="serarch">
<form>
<input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
<input class="submit" type="submit" name="submit" value="" />
</form>
</div>
<div class="login">
<a>登录</a>
<a>注册</a>
</div>
</div>
</div>
<div style="margin-top:55px;"></div>
<div id="register" class="auto">
<h2>欢迎注册成为 私房库会员</h2>
<form method="post">
<label>用户名:<input type="text" name="name" /><span>*用户名不得为空,并且长度不得超过32个字符</span></label>
<label>密码:<input type="password" name="pw" /><span>*密码不得少于6位</span></label>
<label>确认密码:<input type="password" name="confirm_pw" /><span>*请输入与上面一致</span></label>
<label>验证码:<input name="vcode" name="vocode" type="text" /><span>*请输入下方验证码</span></label>
<img class="vcode" src="style/show_code.php.jpg" />
<div style="clear:both;"></div>
<input class="btn" name="submit" type="submit" value="确定注册" />
</form>
</div>
<div id="footer" class="auto">
<div class="bottom">
<a>私房库</a>
</div>
<div class="copyright">Powered by sifangku ©2021 sifangku.com</div>
</div>
</body>
</html>
6.7 补充验证
添加功能:验证用户名是否已经存在。
细节:记得对传入sql的参数,转义。
<?php
if(empty($_POST['name'])){
skip("register.php", "error", "用户名不得为空!");
}
if(mb_strlen($_POST['name']) > 32){
skip("register.php", "error", "用户名不得超过32位!");
}
if(mb_strlen($_POST['pw'] < 6)){
skip("register.php", "error", "密码不得少于6位!");
}
if($_POST['pw'] != $_POST['confirm_pw']){
skip("register.php", "error", "两次输入的密码不一致!");
}
$link = connect();
$_POST = escapes($link, $_POST);
$query = "select * from sfk_member where name='{$_POST['name']}'";
$result = execute($link, $query);
// if(mysqli_affected_rows($link)){//查询语句不用这个
// skip("register.php", "error", "用户名已经存在!");
// }
if(mysqli_num_rows($result)){
skip("register.php", "error", "用户名已经被别人注册了!" );
}
?>
6.8 验证码系统
1.在sfkbbs>font>导入字体包
2.在sfkbbs>inc>创建vcode.inc.php:
<?php
/**
*
* @param number $width 验证码长度
* @param number $height 验证码高度
* @param number $fontSize 验证码字体大小
* @param number $countElement 验证码字符个数
* @param number $countPixel 验证码背景的点的数量
* @param number $countLine 画的线的数量
* @return string
*/
function vcode($width=120,$height=40,$fontSize=30,$countElement=5,$countPixel=100,$countLine=4){
header('Content-type:image/jpeg');
$element=array('a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$string='';
/**
* 在验证码中,显示字符的个数
*/
for ($i=0;$i<$countElement;$i++){
$string.=$element[rand(0,count($element)-1)];
}
$img=imagecreatetruecolor($width, $height);
$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorBorder=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorString=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
imagefill($img,0,0,$colorBg);
/**
* 在验证码中,背景的点的数量
*/
for($i=0;$i<$countPixel;$i++){
imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));
}
/**
* 画的线的数量
*/
for($i=0;$i<$countLine;$i++){
imageline($img,rand(0,$width/2),rand(0,$height),rand($width/2,$width),rand(0,$height),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));
}
//imagestring($img,5,0,0,'abcd',$colorString);
/**
* 字;验证码字体大小;起始位置;
*/
imagettftext($img,$fontSize,rand(-5,5),rand(5,15),rand(30,35),$colorString,'font/ManyGifts.ttf',$string);
imagejpeg($img);
imagedestroy($img);
return $string;
}
?>
可能遇到问题:验证码只有一个小方格
法1:用绝对路径引入字体文件as:
//imagestring($img,5,0,0,'abcd',$colorString);
/**
* 字;验证码字体大小;起始位置;
*/
putenv("GDFONTPATH".realpath("."));
imagettftext($img,$fontSize,rand(-5,5),rand(5,15),rand(30,35),$colorString,'D:\300IT\PHP\Wamp64\www\sfkbbs\font\ManyGifts.ttf',$string);
imagejpeg($img);
imagedestroy($img);
return $string;
法2:相对路径转化为绝对路径,但在该文件中使用的其实还是相对路径as:
//imagestring($img,5,0,0,'abcd',$colorString);
/**
* 字;验证码字体大小;起始位置;
*/
//相对路径转化为绝对路径,但在该文件中使用的其实还是相对路径。
$realpath = realpath("font\ManyGifts.ttf");
imagettftext($img,$fontSize,rand(-5,5),rand(5,15),rand(30,35),$colorString,$realpath,$string);
imagejpeg($img);
imagedestroy($img);
/**
* 返回验证码内容
*/
return $string;
6.9 配置验证码并开启SESSION
在sfkbbs>创建show_code.php
作用:保存验证码的内容到session域中,方便其他地方引用
<?php
session_start();
include_once 'inc/vcode.inc.php';
//保存验证码的内容到session域中
$_SESSION['vcode'] = vcode();
?>
在sfkbbs>config.inc.php配置
作用:开启session服务:让其他页面引入了的也能使用session数据
<?php
//开启session服务:让其他页面引入了的也能使用session数据
session_start();
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '123456');
define('DB_DATABASE', 'sfkbbs');
define('DB_PORT', '3306');
header('Content-type:text/html;charset=utf-8');
6.10 验证码验证
在sfkbbs>inc>check_register.inc.php配置
<?php
if(empty($_POST['name'])){
skip("register.php", "error", "用户名不得为空!");
}
if(mb_strlen($_POST['name']) > 32){
skip("register.php", "error", "用户名不得超过32位!");
}
if(mb_strlen($_POST['pw'] < 6)){
skip("register.php", "error", "密码不得少于6位!");
}
if($_POST['pw'] != $_POST['confirm_pw']){
skip("register.php", "error", "两次输入的密码不一致!");
}
$link = connect();
$_POST = escapes($link, $_POST);
$query = "select * from sfk_member where name='{$_POST['name']}'";
$result = execute($link, $query);
// if(mysqli_affected_rows($link)){//查询语句不用这个
// skip("register.php", "error", "用户名已经存在!");
// }
if(mysqli_num_rows($result)){
skip("register.php", "error", "用户名已经被别人注册了!" );
}
if(empty($_POST['vcode'])){
skip("register.php", "error", "验证码不能为空!");
}
//转化为小写的函数
if (strtolower($_POST['vcode']) != strtolower($_SESSION['vcode'])){
skip("register.php", "error", "验证码输入错误!");
}
?>
6.11 重复注册
6.11.1 COOKIE
在sfkbbs>register.php配置
补充知识点: setcookie(“pw”,sha1(md5($_POST[‘pw’])));//双重加密
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
if(isset($_POST['submit'])){
$link=connect();
include 'inc/check_register.inc.php';
$_POST = escapes($link, $_POST);
$query="insert into sfk_member(name,pw,register_time) values('{$_POST['name']}',md5('{$_POST['pw']}'),now())";
execute($link,$query);
if(mysqli_affected_rows($link)==1){
setcookie("sfk[name]",$_POST['name']);
setcookie("sfk[pw]",sha1(md5($_POST['pw'])));//双重加密
skip('index.php','ok','注册成功!');
}else{
skip('register.php','eror','注册失败,请重试!');
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link rel="stylesheet" type="text/css" href="style/public.css" />
<link rel="stylesheet" type="text/css" href="style/register.css" />
</head>
<body>
<div class="header_wrap">
<div id="header" class="auto">
<div class="logo">sifangku</div>
<div class="nav">
<a class="hover">首页</a>
</div>
<div class="serarch">
<form>
<input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
<input class="submit" type="submit" name="submit" value="" />
</form>
</div>
<div class="login">
<a>登录</a>
<a>注册</a>
</div>
</div>
</div>
<div style="margin-top:55px;"></div>
<div id="register" class="auto">
<h2>欢迎注册成为 私房库会员</h2>
<form method="post">
<label>用户名:<input type="text" name="name" required/><span>*用户名不得为空,并且长度不得超过32个字符</span></label>
<label>密码:<input type="password" name="pw" required /><span>*密码不得少于6位</span></label>
<label>确认密码:<input type="password" name="confirm_pw" required /><span>*请输入与上面一致</span></label>
<label>验证码:<input name="vcode" name="vocode" type="text" required/><span>*请输入下方验证码</span></label>
<img class="vcode" src="show_code.php" />
<div style="clear:both;"></div>
<input class="btn" name="submit" type="submit" value="确定注册" />
</form>
</div>
<div id="footer" class="auto">
<div class="bottom">
<a>私房库</a>
</div>
<div class="copyright">Powered by wuzhideren ©2021 sifangku.com</div>
</div>
</body>
</html>
6.11.2 测试COOKIE
在sfkbbs>register.php配置
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
var_dump($_COOKIE);
if(isset($_POST['submit'])){
$link=connect();
include 'inc/check_register.inc.php';
$_POST = escapes($link, $_POST);
$query="insert into sfk_member(name,pw,register_time) values('{$_POST['name']}',md5('{$_POST['pw']}'),now())";
execute($link,$query);
if(mysqli_affected_rows($link)==1){
setcookie("sfk[name]",$_POST['name']);
setcookie("sfk[pw]",sha1(md5($_POST['pw'])));//双重加密
skip('index.php','ok','注册成功!');
}else{
skip('register.php','eror','注册失败,请重试!');
}
}
?>
若出现下图左上角文字,说明成功(未加密版):
6.11.3 登录状态
在sfkbbs>inc>tool.php
需求:当登录了的用户,访问注册页面,则报错。
<?php
function skip($url,$class,$message){
//!!!注意定界符里面不能解析php标签
//!!!!注意如何在meta里面设置自动跳转语法
$html = <<<A
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="3; URL={$url}"/>
<title>正在跳转</title>
<link rel="stylesheet" type="text/css" href="style/remind.css" />
</head>
<body>
<div class="notice"><span class="pic {$class}"></span>{$message}<a href="{$url}">3秒后自动跳转</div>
</body>
</html>
A;
echo $html;
exit;//不写括号也可以?对
}
/**
* 验证登录是否成功
*/
function is_login($link){
if(isset($_COOKIE['sfk']['name']) && isset($_COOKIE['sfk']['pw'])){
$query="select * from sfk_member where name='{$_COOKIE['sfk']['name']}' and sha1(pw)='{$_COOKIE['sfk']['pw']}'";
$result=execute($link,$query);
if(mysqli_num_rows($result)==1){
$data=mysqli_fetch_assoc($result);
return $data['id'];
}else{
return false;
}
}else{
return false;
}
}
?>
6.11.4 测试注册
在sfkbbs>register.php配置
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
$link=connect();
var_dump(is_login($link));exit();
if(isset($_POST['submit'])){
include 'inc/check_register.inc.php';
$_POST = escapes($link, $_POST);
$query="insert into sfk_member(name,pw,register_time) values('{$_POST['name']}',md5('{$_POST['pw']}'),now())";
execute($link,$query);
if(mysqli_affected_rows($link)==1){
setcookie("sfk[name]",$_POST['name']);
setcookie("sfk[pw]",sha1(md5($_POST['pw'])));//双重加密
skip('index.php','ok','注册成功!');
}else{
skip('register.php','eror','注册失败,请重试!');
}
}
?>
若浏览器显示的是对应的id值,则成功。
7 用户登录
7.1 创建登录页
在sfkbbs>创建login.php
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link rel="stylesheet" type="text/css" href="style/public.css" />
<link rel="stylesheet" type="text/css" href="style/register.css" />
</head>
<body>
<div class="header_wrap">
<div id="header" class="auto">
<div class="logo">sifangku</div>
<div class="nav">
<a class="hover">首页</a>
<a>新帖</a>
<a>话题</a>
</div>
<div class="serarch">
<form>
<input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
<input class="submit" type="submit" name="submit" value="" />
</form>
</div>
<div class="login">
<a>登录</a>
<a>注册</a>
</div>
</div>
</div>
<div style="margin-top:55px;"></div>
<div id="register" class="auto">
<h2>欢迎注册成为 私房库会员</h2>
<form>
<label>用户名:<input type="text" /><span>*用户名含有禁用字符,请选择其他用户名</span></label>
<label>密码:<input type="password" /><span>*用户名含有禁用字符,请选择其他用户名</span></label>
<label>确认密码:<input type="password" /><span>*用户名含有禁用字符,请选择其他用户名</span></label>
<label>验证码:<input name="vcode" type="text" /><span>*请输入下方验证码</span></label>
<img class="vcode" src="style/show_code.php.jpg" />
<label>自动登录:
<select style="width:236px;height:25px;" name="time">
<option value="3600">1小时内</option>
<option value="86400">1天内</option>
<option value="259200">3天内</option>
<option value="2592000">30天内</option>
</select>
<span>*公共电脑上请勿长期自动登录</span>
</label>
<div style="clear:both;"></div>
<input class="btn" type="submit" value="确定注册" />
</form>
</div>
<div id="footer" class="auto">
<div class="bottom">
<a>私房库</a>
</div>
<div class="copyright">Powered by sifangku ©2021 sifangku.com</div>
</div>
</body>
</html>
7.2 封装头部和底部
且导入相关配置
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/header.inc.php';
?>
<div id="register" class="auto">
<h2>欢迎注册成为 私房库会员</h2>
<form>
<label>用户名:<input type="text" /><span>*用户名含有禁用字符,请选择其他用户名</span></label>
<label>密码:<input type="password" /><span>*用户名含有禁用字符,请选择其他用户名</span></label>
<label>确认密码:<input type="password" /><span>*用户名含有禁用字符,请选择其他用户名</span></label>
<label>验证码:<input name="vcode" type="text" /><span>*请输入下方验证码</span></label>
<img class="vcode" src="style/show_code.php.jpg" />
<label>自动登录:
<select style="width:236px;height:25px;" name="time">
<option value="3600">1小时内</option>
<option value="86400">1天内</option>
<option value="259200">3天内</option>
<option value="2592000">30天内</option>
</select>
<span>*公共电脑上请勿长期自动登录</span>
</label>
<div style="clear:both;"></div>
<input class="btn" type="submit" value="确定注册" />
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
7.3 根据不同页面引入不同的配置
比如:css文件、页面窗口标题等等
7.3.1 配置头部
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title><?php echo $template['title']?></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<?php
foreach ($template['css'] as $val){
echo "<link rel='stylesheet' type='text/css' href='{$val}' /> ";//细节:应该在单引号中,引入变量
}
?>
</head>
<body>
<div class="header_wrap">
<div id="header" class="auto">
<div class="logo">sifangku</div>
<div class="nav">
<a class="hover">首页</a>
<a>新帖</a>
<a>话题</a>
</div>
<div class="serarch">
<form>
<input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
<input class="submit" type="submit" name="submit" value="" />
</form>
</div>
<div class="login">
<a>登录</a>
<a>注册</a>
</div>
</div>
</div>
<div style="margin-top:55px;"></div>
<?php
7.3.2 登录页
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/config.inc.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/register.css" );
?>
<?php include_once 'inc/header.inc.php';?>
<div id="register" class="auto">
<h2>请登录私房库会员 </h2>
<form>
<label>用户名:<input type="text" /><span>*用户名含有禁用字符,请选择其他用户名</span></label>
<label>密码:<input type="password" /><span>*用户名含有禁用字符,请选择其他用户名</span></label>
<label>确认密码:<input type="password" /><span>*用户名含有禁用字符,请选择其他用户名</span></label>
<label>验证码:<input name="vcode" type="text" /><span>*请输入下方验证码</span></label>
<img class="vcode" src="style/show_code.php.jpg" />
<label>自动登录:
<select style="width:236px;height:25px;" name="time">
<option value="3600">1小时内</option>
<option value="86400">1天内</option>
<option value="259200">3天内</option>
<option value="2592000">30天内</option>
</select>
<span>*公共电脑上请勿长期自动登录</span>
</label>
<div style="clear:both;"></div>
<input class="btn" type="submit" value="确定注册" />
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
7.3.3注册页
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
$link=connect();
if (is_login($link)){
skip('index.php', 'error', "你已经登录,请不要重复注册!");
}
if(isset($_POST['submit'])){
include 'inc/check_register.inc.php';
$_POST = escapes($link, $_POST);
$query="insert into sfk_member(name,pw,register_time) values('{$_POST['name']}',md5('{$_POST['pw']}'),now())";
execute($link,$query);
if(mysqli_affected_rows($link)==1){
setcookie("sfk[name]",$_POST['name']);
setcookie("sfk[pw]",sha1(md5($_POST['pw'])));//双重加密
skip('index.php','ok','注册成功!');
}else{
skip('register.php','eror','注册失败,请重试!');
}
}
?>
<?php
$template['title'] = "会员注册页";
$template['css'] = array("style/public.css", "style/register.css" );
?>
<?php include_once 'inc/header.inc.php';?>
<div id="register" class="auto">
<h2>欢迎注册成为 私房库会员</h2>
<form method="post">
<label>用户名:<input type="text" name="name" required/><span>*用户名不得为空,并且长度不得超过32个字符</span></label>
<label>密码:<input type="password" name="pw" required /><span>*密码不得少于6位</span></label>
<label>确认密码:<input type="password" name="confirm_pw" required /><span>*请输入与上面一致</span></label>
<label>验证码:<input name="vcode" name="vocode" type="text" required/><span>*请输入下方验证码</span></label>
<img class="vcode" src="show_code.php" />
<div style="clear:both;"></div>
<input class="btn" name="submit" type="submit" value="确定注册" />
</form>
</div>
<div id="footer" class="auto">
<div class="bottom">
<a>私房库</a>
</div>
<div class="copyright">Powered by wuzhideren ©2021 sifangku.com</div>
</div>
</body>
</html>
7.4 修改登录页显示内容
在sfkbbs>login.php配置
- 删除不需要的信息
- 修改信息
- 引入验证码系统
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/config.inc.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/register.css" );
?>
<?php include_once 'inc/header.inc.php';?>
<div id="register" class="auto">
<h2>请登录私房库会员 </h2>
<form>
<label>用户名:<input type="text" /><span>*请输入正确的用户名</span></label>
<label>密码:<input type="password" /><span>*请输入正确的密码</span></label>
<label>验证码:<input name="vcode" type="text" /><span>*请输入下方验证码</span></label>
<img class="vcode" src="show_code.php" />
<label>自动登录:
<select style="width:236px;height:25px;" name="time">
<!--这是秒数:3600 -->
<option value="3600">1小时内</option>
<option value="86400">1天内</option>
<option value="259200">3天内</option>
<option value="2592000">30天内</option>
</select>
<span>*公共电脑上请勿长期自动登录</span>
</label>
<div style="clear:both;"></div>
<input class="btn" type="submit" value="登录" />
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
7.5 数据库登录
7.5.1 测试表单参数
在sfkbbs>login.php配置
步骤细节:1.表单提交方式 2.表单参数
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/config.inc.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/register.css" );
if (isset($_POST['submit'])){
var_dump($_POST);exit();
}
?>
<?php include_once 'inc/header.inc.php';?>
<div id="register" class="auto">
<h2>请登录私房库会员 </h2>
<form method="post">
<label>用户名:<input type="text" name="name" /><span>*请输入正确的用户名</span></label>
<label>密码:<input type="password" name="pw" /><span>*请输入正确的密码</span></label>
<label>验证码:<input name="vcode" type="text" /><span>*请输入下方验证码</span></label>
<img class="vcode" src="show_code.php" />
<label>自动登录:
<select style="width:236px;height:25px;" name="time">
<!--这是秒数:3600 -->
<option value="3600">1小时内</option>
<option value="86400">1天内</option>
<option value="259200">3天内</option>
<option value="2592000">30天内</option>
</select>
<span>*公共电脑上请勿长期自动登录</span>
</label>
<div style="clear:both;"></div>
<input class="btn" type="submit" name="submit" value="登录" />
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
7.5.2 登录验证1
在sfkbbs>inc>创建check_login.inc.php
<?php
if(empty($_POST['name'])){
skip("login,php", "error", "用户名不得为空!");
}
if(mb_strlen($_POST['name']) > 32){
skip("login,php", "error", "用户名不得超过32位!");
}
if(mb_strlen($_POST['pw'] < 6)){
skip("login,php", "error", "密码不得少于6位!");
}
if(empty($_POST['vcode'])){
skip("login,php", "error", "验证码不能为空!");
}
//转化为小写的函数
if (strtolower($_POST['vcode']) != strtolower($_SESSION['vcode'])){
skip("login,php", "error", "验证码输入错误!");
}
if(empty($_POST['time']) || !is_numeric($_POST['time']) || $_POST['time'] > 2592000){
$_POST['time'] = 2592000;
}
?>
7.5.2 登录验证2
在sfkbbs>login.php配置
细节1:@pw=md5(‘{$_POST[‘pw’]}’)别忘了加密。
细节2:别忘了验证码系统:include_once ‘inc/config.inc.php’;
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/config.inc.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/register.css" );
if (isset($_POST['submit'])){
include_once 'inc/check_login.inc.php';
$link = connect();
$_POST = escapes($link, $_POST);
$query = "select * from sfk_member where name='{$_POST['name']}' and pw=md5('{$_POST['pw']}')";
$result = execute($link, $query);
if(mysqli_num_rows($result) == 1){
setcookie("sfk[name]", $_POST['name'], time() + $_POST['time']);//这是过去时间:现在时间+注册时间
setcookie("sfk[pw]", sha1(md5($_POST['pw'])), time() + $_POST['time']);
skip('index.php','ok','登录成功!');
}else{
skip("login.php", "error", "用户名或密码输入错误!");
}
}
?>
<?php include_once 'inc/header.inc.php';?>
<div id="register" class="auto">
<h2>请登录私房库会员 </h2>
<form method="post">
<label>用户名:<input type="text" name="name" /><span>*请输入正确的用户名</span></label>
<label>密码:<input type="password" name="pw" /><span>*请输入正确的密码</span></label>
<label>验证码:<input name="vcode" type="text" /><span>*请输入下方验证码</span></label>
<img class="vcode" src="show_code.php" />
<label>自动登录:
<select style="width:236px;height:25px;" name="time">
<!--这是秒数:3600 -->
<option value="3600">1小时内</option>
<option value="86400">1天内</option>
<option value="259200">3天内</option>
<option value="2592000">30天内</option>
</select>
<span>*公共电脑上请勿长期自动登录</span>
</label>
<div style="clear:both;"></div>
<input class="btn" type="submit" name="submit" value="登录" />
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
7.6 重复登录
7.6.1 在登录页
在sfkbbs>login.php配置
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/config.inc.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/register.css" );
?>
<?php
$link = connect();
if ($member_id = is_login($link)){
skip('index.php','error','你已经登录过了,不要重复登录!');
}
if (isset($_POST['submit'])){
include_once 'inc/check_login.inc.php';
$_POST = escapes($link, $_POST);
$query = "select * from sfk_member where name='{$_POST['name']}' and pw=md5('{$_POST['pw']}')";
$result = execute($link, $query);
if(mysqli_num_rows($result) == 1){
setcookie("sfk[name]", $_POST['name'], time() + $_POST['time']);//这是过去时间:现在时间+注册时间
setcookie("sfk[pw]", sha1(md5($_POST['pw'])), time() + $_POST['time']);
skip('index.php','ok','登录成功!');
}else{
skip("login.php", "error", "用户名或密码输入错误!");
}
}
?>
<?php include_once 'inc/header.inc.php';?>
<div id="register" class="auto">
<h2>请登录私房库会员 </h2>
<form method="post">
<label>用户名:<input type="text" name="name" /><span>*请输入正确的用户名</span></label>
<label>密码:<input type="password" name="pw" /><span>*请输入正确的密码</span></label>
<label>验证码:<input name="vcode" type="text" /><span>*请输入下方验证码</span></label>
<img class="vcode" src="show_code.php" />
<label>自动登录:
<select style="width:236px;height:25px;" name="time">
<!--这是秒数:3600 -->
<option value="3600">1小时内</option>
<option value="86400">1天内</option>
<option value="259200">3天内</option>
<option value="2592000">30天内</option>
</select>
<span>*公共电脑上请勿长期自动登录</span>
</label>
<div style="clear:both;"></div>
<input class="btn" type="submit" name="submit" value="登录" />
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
7.6.2 其他修改
在sfkbbs>inc>header.inc.php配置
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title><?php echo $template['title']?></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<?php
foreach ($template['css'] as $val){
echo "<link rel='stylesheet' type='text/css' href='{$val}' /> ";//细节:应该在单引号中,引入变量
}
?>
</head>
<body>
<div class="header_wrap">
<div id="header" class="auto">
<div class="logo">sifangku</div>
<div class="nav">
<a class="hover">首页</a>
<a>新帖</a>
<a>话题</a>
</div>
<div class="serarch">
<form>
<input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
<input class="submit" type="submit" name="submit" value="" />
</form>
</div>
<div class="login">
<?php
if($member_id){
$str=<<<A
<a>你好!{$_COOKIE['sfk']['name']}</a>
A;
echo $str;
}else{
$str=<<<A
<a href="login.php">登录</a>
<a href="register.php">注册</a>
A;
echo $str;
}
?>
</div>
</div>
</div>
<div style="margin-top:55px;"></div>
<?php
在sfkbbs>register.php
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
$link=connect();
if ($member_id = is_login($link)){
skip('index.php', 'error', "你已经登录,请不要重复注册!");
}
if(isset($_POST['submit'])){
include 'inc/check_register.inc.php';
$_POST = escapes($link, $_POST);
$query="insert into sfk_member(name,pw,register_time) values('{$_POST['name']}',md5('{$_POST['pw']}'),now())";
execute($link,$query);
if(mysqli_affected_rows($link)==1){
setcookie("sfk[name]",$_POST['name']);
setcookie("sfk[pw]",sha1(md5($_POST['pw'])));//双重加密
skip('index.php','ok','注册成功!');
}else{
skip('register.php','eror','注册失败,请重试!');
}
}
?>
<?php
$template['title'] = "会员注册页";
$template['css'] = array("style/public.css", "style/register.css" );
?>
<?php include_once 'inc/header.inc.php';?>
<div id="register" class="auto">
<h2>欢迎注册成为 私房库会员</h2>
<form method="post">
<label>用户名:<input type="text" name="name" required/><span>*用户名不得为空,并且长度不得超过32个字符</span></label>
<label>密码:<input type="password" name="pw" required /><span>*密码不得少于6位</span></label>
<label>确认密码:<input type="password" name="confirm_pw" required /><span>*请输入与上面一致</span></label>
<label>验证码:<input name="vcode" name="vocode" type="text" required/><span>*请输入下方验证码</span></label>
<img class="vcode" src="show_code.php" />
<div style="clear:both;"></div>
<input class="btn" name="submit" type="submit" value="确定注册" />
</form>
</div>
<div id="footer" class="auto">
<div class="bottom">
<a>私房库</a>
</div>
<div class="copyright">Powered by wuzhideren ©2021 sifangku.com</div>
</div>
</body>
</html>
8 发帖
8.1 创建表
知识补充:当修改少,访问多时,用该引擎。
可能问题,解决补充:把datatime类型的字段赋默认值为空。
8.2 发帖页
在sfkbbs>创建publish.php
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link rel="stylesheet" type="text/css" href="style/public.css" />
<link rel="stylesheet" type="text/css" href="style/publish.css" />
</head>
<body>
<div class="header_wrap">
<div id="header" class="auto">
<div class="logo">sifangku</div>
<div class="nav">
<a class="hover">首页</a>
</div>
<div class="serarch">
<form>
<input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
<input class="submit" type="submit" name="submit" value="" />
</form>
</div>
<div class="login">
<a>登录</a>
<a>注册</a>
</div>
</div>
</div>
<div style="margin-top:55px;"></div>
<div id="position" class="auto">
<a>首页</a> > <a>NBA</a> > <a>私房库</a> > 的青蛙地区稳定期望
</div>
<div id="publish">
<form method="post">
<select name="module_id">
<option>请选择一个版块</option>
</select>
<input class="title" placeholder="请输入标题" name="title" type="text" />
<textarea name="content" class="content"></textarea>
<input class="publish" type="submit" name="submit" value="" />
<div style="clear:both;"></div>
</form>
</div>
<div id="footer" class="auto">
<div class="bottom">
<a>私房库</a>
</div>
<div class="copyright">Powered by sifangku ©2021 sifangku.com</div>
</div>
</body>
</html>
8.3 拆分整合
在sfkbbs>publish.php配置
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/config.inc.php';
?>
<?php
$template['title'] = "发帖页";
$template['css'] = array("style/public.css", "style/publish.css" );
?>
<?php include_once 'inc/header.inc.php';?>
<div id="position" class="auto">
<a>首页</a> > <a>发布帖子</a>
</div>
<div id="publish">
<form method="post">
<select name="module_id">
<option>请选择一个版块</option>
</select>
<input class="title" placeholder="请输入标题" name="title" type="text" />
<textarea name="content" class="content"></textarea>
<input class="publish" type="submit" name="submit" value="" />
<div style="clear:both;"></div>
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
8.4 登录验证
在sfkbbs>publish.php配置
需求:根据用户是否登录,在导航栏显示不同的效果。
<?php
$link = connect();
//如果$member_id不等于0.说明已经登录了;为什么不直接用函数?:因为用函数就要访问数据库,为了减少访问。
if (!$member_id = is_login($link)){
skip('login.php','error','请登录后再发帖!');
}
?>
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/config.inc.php';
?>
<?php
$template['title'] = "发帖页";
$template['css'] = array("style/public.css", "style/publish.css" );
?>
<?php
$link = connect();
//如果$member_id不等于0.说明已经登录了;为什么不直接用函数?:因为用函数就要访问数据库,为了减少访问。
if (!$member_id = is_login($link)){
skip('login.php','error','请登录后再发帖!');
}
?>
<?php include_once 'inc/header.inc.php';?>
<div id="position" class="auto">
<a>首页</a> > <a>发布帖子</a>
</div>
<div id="publish">
<form method="post">
<select name="module_id">
<option>请选择一个版块</option>
</select>
<input class="title" placeholder="请输入标题" name="title" type="text" />
<textarea name="content" class="content"></textarea>
<input class="publish" type="submit" name="submit" value="" />
<div style="clear:both;"></div>
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
在sfkbbs>inc>header.inc.php配置
<div class="login">
<?php
if($member_id){
$str=<<<A
<a>你好!{$_COOKIE['sfk']['name']}</a>
A;
echo $str;
}else{
$str=<<<A
<a>登录</a>
<a>注册</a>
A;
echo $str;
}
?>
</div>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title><?php echo $template['title']?></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<?php
foreach ($template['css'] as $val){
echo "<link rel='stylesheet' type='text/css' href='{$val}' /> ";//细节:应该在单引号中,引入变量
}
?>
</head>
<body>
<div class="header_wrap">
<div id="header" class="auto">
<div class="logo">sifangku</div>
<div class="nav">
<a class="hover">首页</a>
<a>新帖</a>
<a>话题</a>
</div>
<div class="serarch">
<form>
<input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
<input class="submit" type="submit" name="submit" value="" />
</form>
</div>
<div class="login">
<?php
if($member_id){
$str=<<<A
<a>你好!{$_COOKIE['sfk']['name']}</a>
A;
echo $str;
}else{
$str=<<<A
<a>登录</a>
<a>注册</a>
A;
echo $str;
}
?>
</div>
</div>
</div>
<div style="margin-top:55px;"></div>
<?php
在sfkbbs_login.php配置
$link = connect();
if ($member_id= is_login($link)){
skip('index.php','error','你已经登录过了,不要重复登录!');
}
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/config.inc.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/register.css" );
?>
<?php
$link = connect();
if ($member_id= is_login($link)){
skip('index.php','error','你已经登录过了,不要重复登录!');
}
if (isset($_POST['submit'])){
include_once 'inc/check_login.inc.php';
$_POST = escapes($link, $_POST);
$query = "select * from sfk_member where name='{$_POST['name']}' and pw=md5('{$_POST['pw']}')";
$result = execute($link, $query);
if(mysqli_num_rows($result) == 1){
setcookie("sfk[name]", $_POST['name'], time() + $_POST['time']);//这是过去时间:现在时间+注册时间
setcookie("sfk[pw]", sha1(md5($_POST['pw'])), time() + $_POST['time']);
skip('index.php','ok','登录成功!');
}else{
skip("login.php", "error", "用户名或密码输入错误!");
}
}
?>
<?php include_once 'inc/header.inc.php';?>
<div id="register" class="auto">
<h2>请登录私房库会员 </h2>
<form method="post">
<label>用户名:<input type="text" name="name" /><span>*请输入正确的用户名</span></label>
<label>密码:<input type="password" name="pw" /><span>*请输入正确的密码</span></label>
<label>验证码:<input name="vcode" type="text" /><span>*请输入下方验证码</span></label>
<img class="vcode" src="show_code.php" />
<label>自动登录:
<select style="width:236px;height:25px;" name="time">
<!--这是秒数:3600 -->
<option value="3600">1小时内</option>
<option value="86400">1天内</option>
<option value="259200">3天内</option>
<option value="2592000">30天内</option>
</select>
<span>*公共电脑上请勿长期自动登录</span>
</label>
<div style="clear:both;"></div>
<input class="btn" type="submit" name="submit" value="登录" />
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
在sfkbbs>publish.php配置
<select name="module_id">
<?php
$query="select * from sfk_father_module order by sort desc";
$result_father=execute($link, $query);
while ($data_father=mysqli_fetch_assoc($result_father)){
echo "<optgroup label='{$data_father['module_name']}'>";
$query="select * from sfk_son_module where father_module_id={$data_father['id']} order by sort desc";
$result_son=execute($link, $query);
while ($data_son=mysqli_fetch_assoc($result_son)){
echo "<option value='{$data_son['id']}'>{$data_son['module_name']}</option>";
}
echo "</optgroup>";
}
?>
</select>
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/config.inc.php';
?>
<?php
$template['title'] = "发帖页";
$template['css'] = array("style/public.css", "style/publish.css" );
?>
<?php
$link = connect();
//如果$member_id不等于0.说明已经登录了;为什么不直接用函数?:因为用函数就要访问数据库,为了减少访问。
if (!$member_id = is_login($link)){
skip('login.php','error','请登录后再发帖!');
}
?>
<?php include_once 'inc/header.inc.php';?>
<div id="position" class="auto">
<a>首页</a> > <a>发布帖子</a>
</div>
<div id="publish">
<form method="post">
<select name="module_id">
<?php
$query="select * from sfk_father_module order by sort desc";
$result_father=execute($link, $query);
while ($data_father=mysqli_fetch_assoc($result_father)){
echo "<optgroup label='{$data_father['module_name']}'>";
$query="select * from sfk_son_module where father_module_id={$data_father['id']} order by sort desc";
$result_son=execute($link, $query);
while ($data_son=mysqli_fetch_assoc($result_son)){
echo "<option value='{$data_son['id']}'>{$data_son['module_name']}</option>";
}
echo "</optgroup>";
}
?>
</select>
<input class="title" placeholder="请输入标题" name="title" type="text" />
<textarea name="content" class="content"></textarea>
<input class="publish" type="submit" name="submit" value="" />
<div style="clear:both;"></div>
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
8.5 发帖验证
在sfkbbs>inc>创建check_public.inc.php
<?php
/**
* 防止恶意在网页源码修改数据
*/
if(empty($_POST['module_id']) || !is_numeric($_POST['module_id'])){
skip('publish.php', 'error', '所属版块id不合法!');
}
$query="select * from sfk_son_module where id={$_POST['module_id']}";
$result=execute($link, $query);
if(mysqli_num_rows($result)!=1){
skip('publish.php', 'error', '所属版块不存在!');
}
if(empty($_POST['title'])){
skip('publish.php', 'error', '标题不得为空!');
}
if(mb_strlen($_POST['title'])>255){
skip('publish.php', 'error', '标题不得超过255个字符!');
}
?>
在sfkbbs>public.php配置
<?php
$link = connect();
//如果$member_id不等于0.说明已经登录了;为什么不直接用函数?:因为用函数就要访问数据库,为了减少访问。
if (!$member_id = is_login($link)){
skip('login.php','error','请登录后再发帖!');
}
if(isset($_POST['submit'])){
include 'inc/check_publish.inc.php';
$_POST=escapes($link,$_POST);
$query="insert into sfk_content(module_id,title,content,time,member_id) values({$_POST['module_id']},'{$_POST['title']}','{$_POST['content']}',now(),{$member_id})";
execute($link, $query);
if(mysqli_affected_rows($link)==1){
skip('publish.php', 'ok', '发布成功!');
}else{
skip('publish.php', 'error', '发布失败,请重试!');
}
}
?>
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/config.inc.php';
?>
<?php
$template['title'] = "发帖页";
$template['css'] = array("style/public.css", "style/publish.css" );
?>
<?php
$link = connect();
//如果$member_id不等于0.说明已经登录了;为什么不直接用函数?:因为用函数就要访问数据库,为了减少访问。
if (!$member_id = is_login($link)){
skip('login.php','error','请登录后再发帖!');
}
if(isset($_POST['submit'])){
include 'inc/check_publish.inc.php';
$_POST=escapes($link,$_POST);
$query="insert into sfk_content(module_id,title,content,time,member_id) values({$_POST['module_id']},'{$_POST['title']}','{$_POST['content']}',now(),{$member_id})";
execute($link, $query);
if(mysqli_affected_rows($link)==1){
skip('publish.php', 'ok', '发布成功!');
}else{
skip('publish.php', 'error', '发布失败,请重试!');
}
}
?>
<?php include_once 'inc/header.inc.php';?>
<div id="position" class="auto">
<a>首页</a> > <a>发布帖子</a>
</div>
<div id="publish">
<form method="post">
<select name="module_id">
<?php
$query="select * from sfk_father_module order by sort desc";
$result_father=execute($link, $query);
while ($data_father=mysqli_fetch_assoc($result_father)){
echo "<optgroup label='{$data_father['module_name']}'>";
$query="select * from sfk_son_module where father_module_id={$data_father['id']} order by sort desc";
$result_son=execute($link, $query);
while ($data_son=mysqli_fetch_assoc($result_son)){
echo "<option value='{$data_son['id']}'>{$data_son['module_name']}</option>";
}
echo "</optgroup>";
}
?>
</select>
<input class="title" placeholder="请输入标题" name="title" type="text" />
<textarea name="content" class="content"></textarea>
<input class="publish" type="submit" name="submit" value="" />
<div style="clear:both;"></div>
</form>
</div>
<?php include_once 'inc/footer.inc.php';?>
9 首页
9.1 创建首页
在sfkbbs>创建index.php
<?php
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
include_once 'inc/config.inc.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/index.css" );
?>
<?php
$link = connect();
$member_id=is_login($link);
?>
<?php include_once 'inc/header.inc.php';?>
<div id="hot" class="auto">
<div class="title">热门动态</div>
<ul class="newlist">
<!-- 20条 -->
<li><a href="#">[库队]</a> <a href="#">私房库实战项目录制中...</a></li>
</ul>
<div style="clear:both;"></div>
</div>
<div class="box auto">
<div class="title">
国际足球
</div>
<div class="classList">
<div style="padding:10px 0;">暂无子版块...</div>
</div>
</div>
<div class="box auto">
<div class="title">
CBA
</div>
<div class="classList">
<div style="padding:10px 0;">暂无子版块...</div>
<div style="clear:both;"></div>
</div>
</div>
<div class="box auto">
<div class="title">
NBA
</div>
<div class="classList">
<div class="childBox new">
<h2><a href="#">私队</a> <span>(今日38)</span></h2>
帖子:1939539<br />
</div>
<div class="childBox old">
<h2><a href="#">房队</a> <span>(今日38)</span></h2>
帖子:1939539<br />
</div>
<div class="childBox lock">
<h2><a href="#">库队</a> <span>(今日38)</span></h2>
帖子:1939539<br />
</div>
<div class="childBox new">
<h2><a href="#">私房库队</a> <span>(今日38)</span></h2>
帖子:1939539<br />
</div>
<div style="clear:both;"></div>
</div>
</div>
<?php include_once 'inc/footer.inc.php';?>
9.2 动态首页
在sfkbbs>index.php配置
知识点补充:CURDATE() 表示当前日期(不包含时分秒)。
知识点补充2:嵌套使用php和html:1.使用定界符(不识别php格式符,但识别{}) 2.拆分且包含。
功能亮点:统计不同时间的帖子数量。
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
$link=connect();
$member_id=is_login($link);
$template['title']='首页';
$template['css']=array('style/public.css','style/index.css');
?>
<?php include 'inc/header.inc.php'?>
<div id="hot" class="auto">
<div class="title">热门动态</div>
<ul class="newlist">
<!-- 20条 -->
<li><a href="#">[库队]</a> <a href="#">私房库实战项目录制中...</a></li>
</ul>
<div style="clear:both;"></div>
</div>
<?php
$query="select * from sfk_father_module order by sort desc";
$result_father=execute($link, $query);
while($data_father=mysqli_fetch_assoc($result_father)){
?>
<div class="box auto">
<div class="title">
<?php echo $data_father['module_name']?>
</div>
<div class="classList">
<?php
$query="select * from sfk_son_module where father_module_id={$data_father['id']}";
$result_son=execute($link, $query);
if(mysqli_num_rows($result_son)){
while ($data_son=mysqli_fetch_assoc($result_son)){
$query="select count(*) from sfk_content where module_id={$data_son['id']} and time > CURDATE()";
$count_today=num($link,$query);
$query="select count(*) from sfk_content where module_id={$data_son['id']}";
$count_all=num($link,$query);
$html=<<<A
<div class="childBox new">
<h2><a href="#">{$data_son['module_name']}</a> <span>(今日{$count_today})</span></h2>
帖子:{$count_all}<br />
</div>
A;
echo $html;
}
}else{
echo '<div style="padding:10px 0;">暂无子版块...</div>';
}
?>
<div style="clear:both;"></div>
</div>
</div>
<?php }?>
<?php include 'inc/footer.inc.php'?>
10 父板块列表页
在sfkbbs>创建list_father.php
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/list.css" );
?>
<?php
$link = connect();
$member_id=is_login($link);
?>
<?php include_once 'inc/header.inc.php';?>
<div id="position" class="auto">
<a>首页</a> > <a>NBA</a>
</div>
<div id="main" class="auto">
<div id="left">
<div class="box_wrap">
<h3>NBA</h3>
<div class="num">
今日:<span>7</span>
总帖:<span>158061</span>
<div class="moderator"> 子版块: <a>NBA</a> <a>CBA</a></div>
</div>
<div class="pages_wrap">
<a class="btn publish" href=""></a>
<div class="pages">
<a>« 上一页</a>
<a>1</a>
<span>2</span>
<a>3</a>
<a>4</a>
<a>...13</a>
<a>下一页 »</a>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div style="clear:both;"></div>
<ul class="postsList">
<li>
<div class="smallPic">
<a href="#">
<img width="45" height="45"src="style/2374101_small.jpg">
</a>
</div>
<div class="subject">
<div class="titleWrap"><a href="#">[分类]</a> <h2><a href="#">我这篇帖子不错哦</a></h2></div>
<p>
楼主:无知的人 2021-12-17 最后回复:2021-12-17
</p>
</div>
<div class="count">
<p>
回复<br /><span>41</span>
</p>
<p>
浏览<br /><span>896</span>
</p>
</div>
<div style="clear:both;"></div>
</li>
<li>
<div class="smallPic">
<a href="#">
<img width="45" height="45"src="style/2374101_small.jpg">
</a>
</div>
<div class="subject">
<div class="titleWrap"><a href="#">[分类]</a> <h2><a href="#">我这篇帖子不错哦</a></h2></div>
<p>
楼主:无知的人 2021-12-17 最后回复:2021-12-17
</p>
</div>
<div class="count">
<p>
回复<br /><span>41</span>
</p>
<p>
浏览<br /><span>896</span>
</p>
</div>
<div style="clear:both;"></div>
</li>
<li>
<div class="smallPic">
<a href="#">
<img width="45" height="45"src="style/2374101_small.jpg">
</a>
</div>
<div class="subject">
<div class="titleWrap"><a href="#">[分类]</a> <h2><a href="#">我这篇帖子不错哦</a></h2></div>
<p>
楼主:无知的人 2021-12-17 最后回复:2021-12-17
</p>
</div>
<div class="count">
<p>
回复<br /><span>41</span>
</p>
<p>
浏览<br /><span>896</span>
</p>
</div>
<div style="clear:both;"></div>
</li>
<li>
<div class="smallPic">
<a href="#">
<img width="45" height="45"src="style/2374101_small.jpg">
</a>
</div>
<div class="subject">
<div class="titleWrap"><a href="#">[分类]</a> <h2><a href="#">我这篇帖子不错哦</a></h2></div>
<p>
楼主:无知的人 2021-12-17 最后回复:2021-12-17
</p>
</div>
<div class="count">
<p>
回复<br /><span>41</span>
</p>
<p>
浏览<br /><span>896</span>
</p>
</div>
<div style="clear:both;"></div>
</li>
</ul>
<div class="pages_wrap">
<a class="btn publish" href=""></a>
<div class="pages">
<a>« 上一页</a>
<a>1</a>
<span>2</span>
<a>3</a>
<a>4</a>
<a>...13</a>
<a>下一页 »</a>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div id="right">
<div class="classList">
<div class="title">版块列表</div>
<ul class="listWrap">
<li>
<h2><a href="#">NBA</a></h2>
<ul>
<li><h3><a href="#">私房库</a></h3></li>
<li><h3><a href="#">私</a></h3></li>
<li><h3><a href="#">房</a></h3></li>
</ul>
</li>
<li>
<h2><a href="#">CBA</a></h2>
</li>
</ul>
</div>
</div>
<div style="clear:both;"></div>
</div>
<?php include_once 'inc/footer.inc.php';?>
10.1 帖页跳转
在sfkbbs>index.php配置
功能:1.点击后页面跳转到对应父板块列表 2.传递id参数
<div class="box auto">
<div class="title">
<a style="color:#105cb6" href="list_father.php?id=<?php echo $data_father['id']?>"><?php echo $data_father['module_name']?></a>
</div>
<div class="classList">
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
$link=connect();
$member_id=is_login($link);
$template['title']='首页';
$template['css']=array('style/public.css','style/index.css');
?>
<?php include 'inc/header.inc.php'?>
<div id="hot" class="auto">
<div class="title">热门动态</div>
<ul class="newlist">
<!-- 20条 -->
<li><a href="#">[库队]</a> <a href="#">私房库实战项目录制中...</a></li>
</ul>
<div style="clear:both;"></div>
</div>
<?php
$query="select * from sfk_father_module order by sort desc";
$result_father=execute($link, $query);
while($data_father=mysqli_fetch_assoc($result_father)){
?>
<div class="box auto">
<div class="title">
<a style="color:#105cb6" href="list_father.php?id=<?php echo $data_father['id']?>"><?php echo $data_father['module_name']?></a>
</div>
<div class="classList">
<?php
$query="select * from sfk_son_module where father_module_id={$data_father['id']}";
$result_son=execute($link, $query);
if(mysqli_num_rows($result_son)){
while ($data_son=mysqli_fetch_assoc($result_son)){
$query="select count(*) from sfk_content where module_id={$data_son['id']} and time > CURDATE()";
$count_today=num($link,$query);
$query="select count(*) from sfk_content where module_id={$data_son['id']}";
$count_all=num($link,$query);
$html=<<<A
<div class="childBox new">
<h2><a href="#">{$data_son['module_name']}</a> <span>(今日{$count_today})</span></h2>
帖子:{$count_all}<br />
</div>
A;
echo $html;
}
}else{
echo '<div style="padding:10px 0;">暂无子版块...</div>';
}
?>
<div style="clear:both;"></div>
</div>
</div>
<?php }?>
<?php include 'inc/footer.inc.php'?>
10.2 验证ID
在sfkbbs>list_father.php配置
功能:验证传入的id参数
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/list.css" );
?>
<?php
/**
* 验证传入的id参数
*/
$link = connect();
$member_id=is_login($link);
if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
skip('index.php', 'error', '父版块id参数不合法!');
}
$query="select * from sfk_father_module where id={$_GET['id']}";
$result_father=execute($link, $query);
if(mysqli_num_rows($result_father)==0){
skip('index.php', 'error', '父版块不存在!');
}
/**
* 统计数量
*/
$data_father=mysqli_fetch_assoc($result_father);
$query="select * from sfk_son_module where father_module_id={$_GET['id']}";
$result_son=execute($link,$query);
$id_son='';
$son_list='';//子版块列表字符串
while($data_son=mysqli_fetch_assoc($result_son)){//一次一次地读取行,直到没有为止,相当于foreach
$id_son.=$data_son['id'].',';
$son_list.="<a>{$data_son['module_name']}</a> ";//子版块列表拼接字符串(别忘了空格)
}
$id_son=trim($id_son,',');
//解决:当其是空字符串,赋值为-1;这样查询结果就是0,因为在数据库中设置了unsigned
if($id_son==''){
$id_son = -1;
}//因为:当新增父板块时,其子板块一定为0,那么再做下面语句一定会出错
$query="select count(*) from sfk_content where module_id in({$id_son})";
$count_all=num($link,$query);
//统计今日发帖数量
$query="select count(*) from sfk_content where module_id in({$id_son}) and time>CURDATE()";
$count_today=num($link,$query);
?>
<?php include_once 'inc/header.inc.php';?>
<div id="position" class="auto">
<a href="index.php">首页</a> > <a href="list_father.php?id=<?php echo $data_father['id']?>"><?php echo $data_father['module_name']?></a>
</div>
<div id="main" class="auto">
<div id="left">
<div class="box_wrap">
<h3><?php echo $data_father['module_name']?></h3>
<div class="num">
今日:<span><?php echo $count_today?></span>
总帖:<span><?php echo $count_all?></span>
<div class="moderator"> 子版块: <?php echo $son_list?></div>
</div>
<div class="pages_wrap">
<a class="btn publish" href=""></a>
<div class="pages">
<a>« 上一页</a>
<a>1</a>
<span>2</span>
<a>3</a>
<a>4</a>
<a>...13</a>
<a>下一页 »</a>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div style="clear:both;"></div>
<ul class="postsList">
<li>
<div class="smallPic">
<a href="#">
<img width="45" height="45"src="style/2374101_small.jpg">
</a>
</div>
<div class="subject">
<div class="titleWrap"><a href="#">[分类]</a> <h2><a href="#">我这篇帖子不错哦</a></h2></div>
<p>
楼主:无知的人 2021-12-17 最后回复:2021-12-17
</p>
</div>
<div class="count">
<p>
回复<br /><span>41</span>
</p>
<p>
浏览<br /><span>896</span>
</p>
</div>
<div style="clear:both;"></div>
</li>
<li>
<div class="smallPic">
<a href="#">
<img width="45" height="45"src="style/2374101_small.jpg">
</a>
</div>
<div class="subject">
<div class="titleWrap"><a href="#">[分类]</a> <h2><a href="#">我这篇帖子不错哦</a></h2></div>
<p>
楼主:无知的人 2021-12-17 最后回复:2021-12-17
</p>
</div>
<div class="count">
<p>
回复<br /><span>41</span>
</p>
<p>
浏览<br /><span>896</span>
</p>
</div>
<div style="clear:both;"></div>
</li>
<li>
<div class="smallPic">
<a href="#">
<img width="45" height="45"src="style/2374101_small.jpg">
</a>
</div>
<div class="subject">
<div class="titleWrap"><a href="#">[分类]</a> <h2><a href="#">我这篇帖子不错哦</a></h2></div>
<p>
楼主:无知的人 2021-12-17 最后回复:2021-12-17
</p>
</div>
<div class="count">
<p>
回复<br /><span>41</span>
</p>
<p>
浏览<br /><span>896</span>
</p>
</div>
<div style="clear:both;"></div>
</li>
<li>
<div class="smallPic">
<a href="#">
<img width="45" height="45"src="style/2374101_small.jpg">
</a>
</div>
<div class="subject">
<div class="titleWrap"><a href="#">[分类]</a> <h2><a href="#">我这篇帖子不错哦</a></h2></div>
<p>
楼主:无知的人 2021-12-17 最后回复:2021-12-17
</p>
</div>
<div class="count">
<p>
回复<br /><span>41</span>
</p>
<p>
浏览<br /><span>896</span>
</p>
</div>
<div style="clear:both;"></div>
</li>
</ul>
<div class="pages_wrap">
<a class="btn publish" href=""></a>
<div class="pages">
<a>« 上一页</a>
<a>1</a>
<span>2</span>
<a>3</a>
<a>4</a>
<a>...13</a>
<a>下一页 »</a>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div id="right">
<div class="classList">
<div class="title">版块列表</div>
<ul class="listWrap">
<li>
<h2><a href="#">NBA</a></h2>
<ul>
<li><h3><a href="#">私房库</a></h3></li>
<li><h3><a href="#">私</a></h3></li>
<li><h3><a href="#">房</a></h3></li>
</ul>
</li>
<li>
<h2><a href="#">CBA</a></h2>
</li>
</ul>
</div>
</div>
<div style="clear:both;"></div>
</div>
<?php include_once 'inc/footer.inc.php';?>
10.3 动态头部
在sfkbbs>list_father.php配置
实现功能:
- 跳转到首页
- 跳转到当前页
- sql语句统计今日、总贴数量:
- 去除字符串首/尾的字符函数:trim()
- 一次统计多个符合不同条件的sql语句
- 子版块小列表
- 细节:防止父板块的子版块为空时,查询语句出错。
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/list.css" );
?>
<?php
/**
* 1 验证传入的id参数
*/
$link = connect();
$member_id=is_login($link);
if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
skip('index.php', 'error', '父版块id参数不合法!');
}
$query="select * from sfk_father_module where id={$_GET['id']}";
$result_father=execute($link, $query);
if(mysqli_num_rows($result_father)==0){
skip('index.php', 'error', '父版块不存在!');
}
/**
* 2 统计数量
* 2.1 统计总发帖
*/
$data_father=mysqli_fetch_assoc($result_father);
$query="select * from sfk_son_module where father_module_id={$_GET['id']}";
$result_son=execute($link,$query);
$id_son='';
$son_list='';//子版块列表字符串
while($data_son=mysqli_fetch_assoc($result_son)){//一次一次地读取行,直到没有为止,相当于foreach
$id_son.=$data_son['id'].',';
$son_list.="<a>{$data_son['module_name']}</a> ";//子版块列表拼接字符串(别忘了空格)
}
$id_son=trim($id_son,',');
//解决:当其是空字符串,赋值为-1;这样查询结果就是0,因为在数据库中设置了unsigned
if($id_son==''){
$id_son = -1;
}//因为:当新增父板块时,其子板块一定为0,那么再做下面语句一定会出错
$query="select count(*) from sfk_content where module_id in({$id_son})";
$count_all=num($link,$query);
/*
* 2.2 统计今日发帖数量
*/
$query="select count(*) from sfk_content where module_id in({$id_son}) and time>CURDATE()";
$count_today=num($link,$query);
?>
<?php include_once 'inc/header.inc.php';?>
<div id="position" class="auto">
<a href="index.php">首页</a> > <a href="list_father.php?id=<?php echo $data_father['id']?>"><?php echo $data_father['module_name']?></a>
</div>
<div id="main" class="auto">
<div id="left">
<div class="box_wrap">
<h3><?php echo $data_father['module_name']?></h3>
<div class="num">
今日:<span><?php echo $count_today?></span>
总帖:<span><?php echo $count_all?></span>
<div class="moderator"> 子版块: <?php echo $son_list?></div>
</div>
<div class="pages_wrap">
<a class="btn publish" href=""></a>
<div class="pages">
<a>« 上一页</a>
<a>1</a>
<span>2</span>
<a>3</a>
<a>4</a>
<a>...13</a>
<a>下一页 »</a>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div style="clear:both;"></div>
<ul class="postsList">
<li>
<div class="smallPic">
<a href="#">
<img width="45" height="45"src="style/2374101_small.jpg">
</a>
</div>
<div class="subject">
<div class="titleWrap"><a href="#">[分类]</a> <h2><a href="#">我这篇帖子不错哦</a></h2></div>
<p>
楼主:孙胜利 2021-12-20 最后回复:2021-12-20
</p>
</div>
<div class="count">
<p>
回复<br /><span>41</span>
</p>
<p>
浏览<br /><span>896</span>
</p>
</div>
<div style="clear:both;"></div>
</li>
</ul>
<div class="pages_wrap">
<a class="btn publish" href=""></a>
<div class="pages">
<a>« 上一页</a>
<a>1</a>
<span>2</span>
<a>3</a>
<a>4</a>
<a>...13</a>
<a>下一页 »</a>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div id="right">
<div class="classList">
<div class="title">版块列表</div>
<ul class="listWrap">
<li>
<h2><a href="#">NBA</a></h2>
<ul>
<li><h3><a href="#">私房库</a></h3></li>
<li><h3><a href="#">私</a></h3></li>
<li><h3><a href="#">房</a></h3></li>
</ul>
</li>
<li>
<h2><a href="#">CBA</a></h2>
</li>
</ul>
</div>
</div>
<div style="clear:both;"></div>
</div>
<?php include_once 'inc/footer.inc.php';?>
10.4 动态列表
在sfkbbs>list_father.php配置
功能:1.动态显示帖子的信息
- 多表查询细节:获取字段时,要显式地获取字段。
- 细节:前往数据库,修改访问次数默认值为:0;若不能直接修改,则删除该字段后重建。,否则,页面显示的访问次数为空,不为0。
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/list.css" );
?>
<?php
/**
* 1 验证传入的id参数
*/
$link = connect();
$member_id=is_login($link);
if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
skip('index.php', 'error', '父版块id参数不合法!');
}
$query="select * from sfk_father_module where id={$_GET['id']}";
$result_father=execute($link, $query);
if(mysqli_num_rows($result_father)==0){
skip('index.php', 'error', '父版块不存在!');
}
/**
* 2 统计数量
* 2.1 统计总发帖
*/
$data_father=mysqli_fetch_assoc($result_father);
$query="select * from sfk_son_module where father_module_id={$_GET['id']}";
$result_son=execute($link,$query);
$id_son='';
$son_list='';//子版块列表字符串
while($data_son=mysqli_fetch_assoc($result_son)){//一次一次地读取行,直到没有为止,相当于foreach
$id_son.=$data_son['id'].',';
$son_list.="<a>{$data_son['module_name']}</a> ";//子版块列表拼接字符串(别忘了空格)
}
$id_son=trim($id_son,',');
//解决:当其是空字符串,赋值为-1;这样查询结果就是0,因为在数据库中设置了unsigned
if($id_son==''){
$id_son = -1;
}//因为:当新增父板块时,其子板块一定为0,那么再做下面语句一定会出错
$query="select count(*) from sfk_content where module_id in({$id_son})";
$count_all=num($link,$query);
/*
* 2.2 统计今日发帖数量
*/
$query="select count(*) from sfk_content where module_id in({$id_son}) and time>CURDATE()";
$count_today=num($link,$query);
?>
<?php include_once 'inc/header.inc.php';?>
<div id="position" class="auto">
<a href="index.php">首页</a> > <a href="list_father.php?id=<?php echo $data_father['id']?>"><?php echo $data_father['module_name']?></a>
</div>
<div id="main" class="auto">
<div id="left">
<div class="box_wrap">
<h3><?php echo $data_father['module_name']?></h3>
<div class="num">
今日:<span><?php echo $count_today?></span>
总帖:<span><?php echo $count_all?></span>
<div class="moderator"> 子版块: <?php echo $son_list?></div>
</div>
<div class="pages_wrap">
<a class="btn publish" href=""></a>
<div class="pages">
<a>« 上一页</a>
<a>1</a>
<span>2</span>
<a>3</a>
<a>4</a>
<a>...13</a>
<a>下一页 »</a>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div style="clear:both;"></div>
<ul class="postsList">
<?php
$query="select
sfk_content.title,sfk_content.id,sfk_content.time,sfk_content.times,sfk_member.name,sfk_member.photo,sfk_son_module.module_name
from sfk_content,sfk_member,sfk_son_module where
sfk_content.module_id in({$id_son}) and
sfk_content.member_id=sfk_member.id and
sfk_content.module_id=sfk_son_module.id";
$result_content=execute($link,$query);
while($data_content=mysqli_fetch_assoc($result_content)){
?>
<li>
<div class="smallPic">
<a href="#">
<img width="45" height="45"src="<?php if($data_content['photo']!=''){echo $data_content['photo'];}else{echo 'style/photo.jpg';}?>">
</a>
</div>
<div class="subject">
<div class="titleWrap"><a href="#">[<?php echo $data_content['module_name']?>]</a> <h2><a href="#"><?php echo $data_content['title']?></a></h2></div>
<p>
楼主:<?php echo $data_content['name']?> <?php echo $data_content['time']?> 最后回复:2021-12-20
</p>
</div>
<div class="count">
<p>
回复<br /><span>41</span>
</p>
<p>
浏览<br /><span><?php echo $data_content['times']?></span>
</p>
</div>
<div style="clear:both;"></div>
</li>
<?php
}
?>
</ul>
<div class="pages_wrap">
<a class="btn publish" href=""></a>
<div class="pages">
<a>« 上一页</a>
<a>1</a>
<span>2</span>
<a>3</a>
<a>4</a>
<a>...13</a>
<a>下一页 »</a>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div id="right">
<div class="classList">
<div class="title">版块列表</div>
<ul class="listWrap">
<li>
<h2><a href="#">NBA</a></h2>
<ul>
<li><h3><a href="#">私房库</a></h3></li>
<li><h3><a href="#">私</a></h3></li>
<li><h3><a href="#">房</a></h3></li>
</ul>
</li>
<li>
<h2><a href="#">CBA</a></h2>
</li>
</ul>
</div>
</div>
<div style="clear:both;"></div>
</div>
<?php include_once 'inc/footer.inc.php';?>
10.5 版块列表
在sfkbbs>list_father.php配置
功能:版块列表的父版块跳转
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
?>
<?php
$template['title'] = "登录页";
$template['css'] = array("style/public.css", "style/list.css" );
?>
<?php
/**
* 1 验证传入的id参数
*/
$link = connect();
$member_id=is_login($link);
if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
skip('index.php', 'error', '父版块id参数不合法!');
}
$query="select * from sfk_father_module where id={$_GET['id']}";
$result_father=execute($link, $query);
if(mysqli_num_rows($result_father)==0){
skip('index.php', 'error', '父版块不存在!');
}
/**
* 2 统计数量
* 2.1 统计总发帖
*/
$data_father=mysqli_fetch_assoc($result_father);
$query="select * from sfk_son_module where father_module_id={$_GET['id']}";
$result_son=execute($link,$query);
$id_son='';
$son_list='';//子版块列表字符串
while($data_son=mysqli_fetch_assoc($result_son)){//一次一次地读取行,直到没有为止,相当于foreach
$id_son.=$data_son['id'].',';
$son_list.="<a>{$data_son['module_name']}</a> ";//子版块列表拼接字符串(别忘了空格)
}
$id_son=trim($id_son,',');
//解决:当其是空字符串,赋值为-1;这样查询结果就是0,因为在数据库中设置了unsigned
if($id_son==''){
$id_son = -1;
}//因为:当新增父板块时,其子板块一定为0,那么再做下面语句一定会出错
$query="select count(*) from sfk_content where module_id in({$id_son})";
$count_all=num($link,$query);
/*
* 2.2 统计今日发帖数量
*/
$query="select count(*) from sfk_content where module_id in({$id_son}) and time>CURDATE()";
$count_today=num($link,$query);
?>
<?php include_once 'inc/header.inc.php';?>
<div id="position" class="auto">
<a href="index.php">首页</a> > <a href="list_father.php?id=<?php echo $data_father['id']?>"><?php echo $data_father['module_name']?></a>
</div>
<div id="main" class="auto">
<div id="left">
<div class="box_wrap">
<h3><?php echo $data_father['module_name']?></h3>
<div class="num">
今日:<span><?php echo $count_today?></span>
总帖:<span><?php echo $count_all?></span>
<div class="moderator"> 子版块: <?php echo $son_list?></div>
</div>
<div class="pages_wrap">
<a class="btn publish" href=""></a>
<div class="pages">
<a>« 上一页</a>
<a>1</a>
<span>2</span>
<a>3</a>
<a>4</a>
<a>...13</a>
<a>下一页 »</a>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div style="clear:both;"></div>
<ul class="postsList">
<?php
$query="select
sfk_content.title,sfk_content.id,sfk_content.time,sfk_content.times,sfk_member.name,sfk_member.photo,sfk_son_module.module_name
from sfk_content,sfk_member,sfk_son_module where
sfk_content.module_id in({$id_son}) and
sfk_content.member_id=sfk_member.id and
sfk_content.module_id=sfk_son_module.id";
$result_content=execute($link,$query);
while($data_content=mysqli_fetch_assoc($result_content)){
?>
<li>
<div class="smallPic">
<a href="#">
<img width="45" height="45"src="<?php if($data_content['photo']!=''){echo $data_content['photo'];}else{echo 'style/photo.jpg';}?>">
</a>
</div>
<div class="subject">
<div class="titleWrap"><a href="#">[<?php echo $data_content['module_name']?>]</a> <h2><a href="#"><?php echo $data_content['title']?></a></h2></div>
<p>
楼主:<?php echo $data_content['name']?> <?php echo $data_content['time']?> 最后回复:2021-12-21
</p>
</div>
<div class="count">
<p>
回复<br /><span>41</span>
</p>
<p>
浏览<br /><span><?php echo $data_content['times']?></span>
</p>
</div>
<div style="clear:both;"></div>
</li>
<?php
}
?>
</ul>
<div class="pages_wrap">
<a class="btn publish" href=""></a>
<div class="pages">
<a>« 上一页</a>
<a>1</a>
<span>2</span>
<a>3</a>
<a>4</a>
<a>...13</a>
<a>下一页 »</a>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div id="right">
<div class="classList">
<div class="title">版块列表</div>
<ul class="listWrap">
<?php
$query="select * from sfk_father_module";
$result_father=execute($link, $query);
while($data_father=mysqli_fetch_assoc($result_father)){
?>
<li>
<h2><a href="list_father.php?id=<?php echo $data_father['id']?>"><?php echo $data_father['module_name']?></a></h2>
<ul>
<?php
$query="select * from sfk_son_module where father_module_id={$data_father['id']}";
$result_son=execute($link, $query);
while($data_son=mysqli_fetch_assoc($result_son)){
?>
<li><h3><a href="#"><?php echo $data_son['module_name']?></a></h3></li>
<?php
}
?>
</ul>
</li>
<?php
}
?>
</ul>
</div>
</div>
<div style="clear:both;"></div>
</div>
<?php include_once 'inc/footer.inc.php';?>
10.6 分页效果
10.6.1 SQL条件分页
在sfkbbs>创建test.php
作用:得到数据库分页条件
<?php
header("Content-type:text/html;charset=utf-8");
/**
* 得到数据库分页条件
* @param unknown $count 总行数
* @param unknown $page_size 一页的数量
* @param unknown $page 页序
* @return string[]
*/
function page($count,$page_size,$page){
/**
* 验证传入的页序,防止sql注入。
* 当传入错误的页序,页面都跳转到第一页
*/
if(!isset($_GET[$page]) || !is_numeric($_GET[$page]) || $_GET[$page]<1){
$_GET[$page]=1;
}
//总页数
$page_num_all=ceil($count/$page_size);//这个函数的作用是:向上取整数。
if($_GET[$page]>$page_num_all){
$_GET[$page]=$page_num_all;
}
$start=($_GET[$page]-1)*$page_size;
$limit="limit {$start},{$page_size}";
echo $_GET[$page];
$data=array(
'limit'=>$limit,
'html'=>'计算出的html代码'
);
return $data;
}
var_dump(page(100,10,'page'));
?>
10.6.2 HTML代码分页
<?php
header("Content-type:text/html;charset=utf-8");
/**
* 得到数据库分页条件
* @param unknown $count 总行数
* @param unknown $page_size 一页的数量
* @param unknown $page 页序
* @return string[]
*/
function page($count,$page_size,$num_btn=10,$page='page'){
if(!isset($_GET[$page]) || !is_numeric($_GET[$page]) || $_GET[$page]<1){
$_GET[$page]=1;
}
//总页数
$page_num_all=ceil($count/$page_size);
if($_GET[$page]>$page_num_all){
$_GET[$page]=$page_num_all;
}
$start=($_GET[$page]-1)*$page_size;
$limit="limit {$start},{$page_size}";
echo '当前页:'.$_GET[$page].'<br />';
$html='';
/**
* 控制按钮显示的数量。
*/
if($num_btn>=$page_num_all){
//把所有的页码按钮全部显示
for($i=1;$i<=$page_num_all;$i++){//这边的$page_num_all是限制循环次数以控制显示按钮数目的变量,$i是记录页码号
/**
* 问题:
* 设置当前页的按钮不是a标签。
*/
if($_GET[$page]==$i){
$html.="<span>{$i}</span> ";
}else{
$html.="<a href='test.php?page={$i}'>{$i}</a> ";
}
}
}else{
$num_left=floor(($num_btn-1)/2);
$start=$_GET[$page]-$num_left;
$end=$start+($num_btn-1);
echo '结束按钮号'.$end.'<br />';
/**
* 问题:当开始的按钮次序过小
*/
if($start<1){
$start=1;
}
/**
* 问题:当结尾的按钮次序过大
* 重新设置开始的按钮次序间接地设置(不应该直接设置结尾的按钮次序)
*/
if($end>$page_num_all){
$start=$page_num_all-($num_btn-1);
}
for($i=0;$i<$num_btn;$i++){
if($_GET[$page]==$start){
$html.="<span>{$start}</span> ";
}else{
$html.="<a href='test.php?page={$start}'>{$start}</a> ";
}
$start++;
}
}
$data=array(
'limit'=>$limit,
'html'=>$html
);
return $data;
}
$page=page(100,10,3);
echo $page['html'];
?>
10.6.3 按钮省略号
<?php
header("Content-type:text/html;charset=utf-8");
/*
参数说明:
$count:总记录数
$page_size:每页显示的记录数
$num_btn:要展示的页码按钮数目
$page:分页的get参数
*/
function page($count,$page_size,$num_btn=10,$page='page'){
if(!isset($_GET[$page]) || !is_numeric($_GET[$page]) || $_GET[$page]<1){
$_GET[$page]=1;
}
//总页数
$page_num_all=ceil($count/$page_size);
if($_GET[$page]>$page_num_all){
$_GET[$page]=$page_num_all;
}
$start=($_GET[$page]-1)*$page_size;
$limit="limit {$start},{$page_size}";
echo '当前页:'.$_GET[$page].'<br />';
$html=array();
/**
* 控制按钮显示的数量。
*/
if($num_btn>=$page_num_all){
//把所有的页码按钮全部显示
for($i=1;$i<=$page_num_all;$i++){//这边的$page_num_all是限制循环次数以控制显示按钮数目的变量,$i是记录页码号
/**
* 问题:
* 设置当前页的按钮不是a标签。
*/
if($_GET[$page]==$i){
$html[$i]="<span>{$i}</span>";
}else{
$html[$i]="<a href='test.php?page={$i}'>{$i}</a>";
}
}
}else{
$num_left=floor(($num_btn-1)/2);
$start=$_GET[$page]-$num_left;
$end=$start+($num_btn-1);
echo '结束按钮号'.$end.'<br />';
/**
* 问题:当开始的按钮次序过小
*/
if($start<1){
$start=1;
}
/**
* 问题:当结尾的按钮次序过大
* 重新设置开始的按钮次序间接地设置(不应该直接设置结尾的按钮次序)
*/
if($end>$page_num_all){
$start=$page_num_all-($num_btn-1);
}
for($i=0;$i<$num_btn;$i++){
if($_GET[$page]==$start){
$html[$start]="<span>{$start}</span>";
}else{
$html[$start]="<a href='test.php?page={$start}'>{$start}</a>";
}
$start++;
}
/**
* 当按钮数量大于3时,作省略号按钮。
*/
if(count($html)>=3){
reset($html);//重置指针
$key_first=key($html);//获取数组的第一个内容
end($html);
$key_end=key($html);//获取数组的最后一个内容
//当第一个按钮次序不是1
if($key_first!=1){
array_shift($html);//移除数组的第一个内容,并且指针重置。
array_unshift($html,"<a href='test.php?page=1'>1...</a>");//在数组开头插入内容
}
//当最后一个按钮次序不等于最大按钮次序(按钮次序==页序)
if($key_end!=$page_num_all){
array_pop($html);//弹出数组的最后一个内容
array_push($html,"<a href='test.php?page={$page_num_all}'>...{$page_num_all}</a>");//在数组最后位置插入内容
}
}
}
/**
* 上下页按钮
*/
//当 当前页不是1时
if($_GET[$page]!=1){
$prev=$_GET[$page]-1;
array_unshift($html,"<a href='test.php?page={$prev}'><< 上一页</a>");
}
//当 当前页不是最后一页时
if($_GET[$page]!=$page_num_all){
$next=$_GET[$page]+1;
array_push($html,"<a href='test.php?page={$next}'>下一页>>> </a>");
}
$html=implode(' ',$html);//拼接数组为字符串
$data=array(
'limit'=>$limit,
'html'=>$html
);
return $data;
}
$page=page(100,10,9);
echo $page['html'];
?>
10.6.4 分页实现
删除sfkbbs>text.php
创建sfkbbs>inc>page.inc.php
功能:通用的分页函数
细节:
如果父版块没有子版块不做分页,
若没有这个判断,则可能分页的头部为以前设置的-1,而导致sql语句错误
<?php
/*
调用:$page=page(100,10,9);
返回值:array('limit','html')
参数说明:
$count:总记录数
$page_size:每页显示的记录数
$num_btn:要展示的页码按钮数目
$page:分页的get参数
*/
function page($count,$page_size,$num_btn=10,$page='page'){
/**
* 如果父版块没有子版块不做分页
*
*/
if ($count == 0){
$data=array(
'limit'=>"",
'html'=>""
);
return $data;
}
if(!isset($_GET[$page]) || !is_numeric($_GET[$page]) || $_GET[$page]<1){
$_GET[$page]=1;
}
//总页数
$page_num_all=ceil($count/$page_size);
if($_GET[$page]>$page_num_all){
$_GET[$page]=$page_num_all;
}
$start=($_GET[$page]-1)*$page_size;
$limit="limit {$start},{$page_size}";
$current_url=$_SERVER['REQUEST_URI'];//获取当前url地址
$arr_current=parse_url($current_url);//将当前url拆分到数组里面
$current_path=$arr_current['path'];//将文件路径部分保存起来
$url='';
if(isset($arr_current['query'])){
parse_str($arr_current['query'],$arr_query);
unset($arr_query[$page]);
if(empty($arr_query)){
$url="{$current_path}?{$page}=";
}else{
$other=http_build_query($arr_query);
$url="{$current_path}?{$other}&{$page}=";
}
}else{
$url="{$current_path}?{$page}=";
}
$html=array();
if($num_btn>=$page_num_all){
//把所有的页码按钮全部显示
for($i=1;$i<=$page_num_all;$i++){//这边的$page_num_all是限制循环次数以控制显示按钮数目的变量,$i是记录页码号
if($_GET[$page]==$i){
$html[$i]="<span>{$i}</span>";
}else{
$html[$i]="<a href='{$url}{$i}'>{$i}</a>";
}
}
}else{
$num_left=floor(($num_btn-1)/2);
$start=$_GET[$page]-$num_left;
$end=$start+($num_btn-1);
if($start<1){
$start=1;
}
if($end>$page_num_all){
$start=$page_num_all-($num_btn-1);
}
for($i=0;$i<$num_btn;$i++){
if($_GET[$page]==$start){
$html[$start]="<span>{$start}</span>";
}else{
$html[$start]="<a href='{$url}{$start}'>{$start}</a>";
}
$start++;
}
//如果按钮数目大于等于3的时候做省略号效果
if(count($html)>=3){
reset($html);
$key_first=key($html);
end($html);
$key_end=key($html);
if($key_first!=1){
array_shift($html);
array_unshift($html,"<a href='{$url}=1'>1...</a>");
}
if($key_end!=$page_num_all){
array_pop($html);
array_push($html,"<a href='{$url}={$page_num_all}'>...{$page_num_all}</a>");
}
}
}
if($_GET[$page]!=1){
$prev=$_GET[$page]-1;
array_unshift($html,"<a href='{$url}{$prev}'>« 上一页</a>");
}
if($_GET[$page]!=$page_num_all){
$next=$_GET[$page]+1;
array_push($html,"<a href='{$url}{$next}'>下一页 »</a>");
}
$html=implode(' ',$html);
$data=array(
'limit'=>$limit,
'html'=>$html
);
return $data;
}
?>
11 退出登录
11.1 退出按钮
在sfkbbs>inc>header.inc.php配置
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title><?php echo $template['title']?></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<?php
foreach ($template['css'] as $val){
echo "<link rel='stylesheet' type='text/css' href='{$val}' /> ";//细节:应该在单引号中,引入变量
}
?>
</head>
<body>
<div class="header_wrap">
<div id="header" class="auto">
<div class="logo">sifangku</div>
<div class="nav">
<a class="hover">首页</a>
<a>新帖</a>
<a>话题</a>
</div>
<div class="serarch">
<form>
<input class="keyword" type="text" name="keyword" placeholder="搜索其实很简单" />
<input class="submit" type="submit" name="submit" value="" />
</form>
</div>
<div class="login">
<?php
if($member_id){
$str=<<<A
<a>你好!{$_COOKIE['sfk']['name']}</a> <span style="color:#fff;">|</span> <a href="logout.php">退出</a>
A;
echo $str;
}else{
$str=<<<A
<a href="login.php">登录</a>
<a href="register.php">注册</a>
A;
echo $str;
}
?>
</div>
</div>
</div>
<div style="margin-top:55px;"></div>
<?php
11.2 清除cookie
在sfkbbs>创建loginout.php
<?php
include_once 'inc/config.inc.php';
include_once 'inc/mysql.inc.php';
include_once 'inc/tool.php';
$link=connect();
$member_id=is_login($link);
if(!$member_id){
skip('index.php','error','你没有登录,不需要退出!');
}
setcookie('sfk[name]','',time()-3600);
setcookie('sfk[pw]','',time()-3600);
skip('index.php','ok','退出成功!');
?>
如果要转载,务必同时把参考资料源处转载
视频地址
- https://www.bilibili.com/video/BV1ct411S7nj?spm_id_from=333.337.search-card.all.click