PHP初级练习实战之留言板
初学者做的东西,有的地方写的不好,哈哈哈!
一.知识重点
1.三目运算 $page= empty($_GET['p']) ? 1: $_GET['p'];
2.数据库的操作mysqli的方法
3.html css js
4.字符串的拼接
5.制作分页页码
6.完整验证码的制作
7.类的运用
8.session传递验证码的方法

二 .代码
代码分为6个文件,liuyanbook.php为主页

1.liuyanbook.php主界面

<?php
//日期
$weekday = ["日","一","二","三","四","五","六"];
date_default_timezone_set('Asia/Shanghai');
$now = getdate(time());
$week=$now['wday'];
$date = date("Y年m月d日  星期$weekday[$week]");
//echo $date;//设置页码变量
//默认首页p=1
$page= empty($_GET['p']) ? 1: $_GET['p'];
//页码
$showPage = 3;
//计算偏移量
$pageoffset = ($showPage-1)/2;//连接数据库,取数据
$host = "127.0.0.1";
$dbuser = "root";
$password = "";
$dbname = "php10";
$db = new mysqli($host, $dbuser, $password, $dbname);if ($db->connect_errno != 0) {echo "连接失败:";echo $db->connect_error;exit;
}//分页
$db->query("set names utf8");
$limit =  ($page-1) * 5;
$sql = "select * from msg order by id desc limit {$limit},5";$mysqli_result = $db->query($sql);
if ($mysqli_result == false) {echo "SQL错误!";exit;
}
//var_dump($mysqli_result);//总页数
$total_sql = "select count(*) from msg";
$total_result = $db->query($total_sql);
$total_array = mysqli_fetch_array($total_result,MYSQLI_ASSOC);
$total = $total_array["count(*)"];
//echo $total;//计算页数
$total_pages = ceil($total/5);
//echo $total_pages;
?><!DOCTYPE html>
<html>
<head><meta charset="UTF-8"/><title>三态电子商务有限公司</title><link rel="stylesheet" type="text/css" href="style.css"/><script type="text/javascript"></script>
</head>
<body>
<div class="contentout"><div class="date"><?php echo $date;?></div><div class="h"><h1>三态电子商务有限公司</h1><h2>留言板</h2></div><div class="all"><div class="add"><form action="liuyan.php" method="post"><textarea name="content" class="content" cols="60" rows="8"></textarea><br>留言人:<input style="padding:6px 8px"; name="user" class="user" type="text"/><p>验证码图片:<img id="captcha_img" border="1" src="str.php?r=<?php echo rand();?>" width="100px" height="30"</p><a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='str.php?r='+Math.random()">换一个?</a>请输入图片中的内容:<input style="padding:6px 8px"; type="text" name="authcode" value="" /><input class="btn" type="submit" value="提交留言"/></div></form><!--留言内容--><?phpwhile($row = mysqli_fetch_array($mysqli_result,MYSQLI_ASSOC)) {?><div class="msg"><div class="info"><span class="user"><?php echo $row["user"];?></span><span class="num"><?php echo "&nbsp;&nbsp;".$row["id"]."楼";?></span><span class="time"><?php echo date("Y-m-d H:i:s",$row["intime"]);?></span>  </div><div class="content"><?php echo $row["content"];?>   </div></div><?php}?></div>//页码部分<div class="page"><?php$page_banner = "";if ($page > 1) {$page_banner = "<a href='liuyanbook.php?p=1'>首页</a>";$page_banner.= "&nbsp;&nbsp;"."<a href='liuyanbook.php?p=".($page-1)."'>上一页</a>";}//初始化前面页码数据$start = 1;$end = $total_pages;if ($total_pages > $showPage) {if($page > $pageoffset + 1) {$page_banner.="...";}if ($page > $pageoffset) {$start = $page - $pageoffset;$end = $total_pages > $page + $pageoffset ? $page + $pageoffset : $total_pages;} else {$start = 1;$end = $total_pages > $showPage ? $showPage : $total_pages;}if ($page + $pageoffset > $total_pages) {$start = $start - ($page + $pageoffset - $total_pages);}}//显示前面的页码for($i=$start;$i<$end;$i++) {$page_banner.= "&nbsp;&nbsp;"."<a href='".$_SERVER['PHP_SELF']."?p=".$i."'>{$i}</a>";}//显示尾部省略if ($total_pages > $showPage && $total_pages > $page + $pageoffset) {$page_banner.="...";}if ($page < $total_pages) {$page_banner.= "&nbsp;&nbsp;"."<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页</a>";$page_banner.= "&nbsp;&nbsp;"."<a href='liuyanbook.php?p=".$total_pages."'>尾页</a>";}//页码部分字符拼接$page_banner.= "&nbsp;&nbsp;"."共".$total_pages."页";$page_banner.= "&nbsp;&nbsp;"."共".$total."条留言";$page_banner.= "<form action='liuyanbook.php' method='get' style= display:inline>";$page_banner.= "&nbsp;&nbsp;"."跳转到第<input type='text' size='1' name='p'>页";$page_banner.= "<input type='submit' value='确定'>";$page_banner.= "</form>";echo  $page_banner;?></div>
</div>
</body>
</html>

2.liuyan.php后端验证,插入数据

<?php
include('class_input.php');
include('dbcontent.php');
//include('str.php');$content = $_POST["content"];
$user = $_POST["user"];
$authcode = $_POST["authcode"];//不能为空
/*if ($content == "") {die("留言内容不能为空!");
}
if ($user == "") {die("请输入留言人的姓名!");}*///对输入的内容和留言人进行检测,不能含有李星和王瑶两个名字
$input = new input();//检测显示的结果$is = $input->post($content);
if ($is == false) {die("留言内容不能为空或者存在禁止关键字!");
}$is = $input->post($user);
if ($is == false) {die("留言人不能为空或者存在禁止关键字!");
}//检测验证码
$codeinput = new codeinput();$iscode = $codeinput->check($authcode);
if ($iscode == false) {die("请输入正确的验证码!");
}//var_dump($content,$user);//将留言内容写入数据库
$time = time();
$sql = "insert into msg (content, user, intime) values ('{$content}','{$user}','{$time}')";
//echo $sql;$is = $db->query($sql);
//var_dump($is);
header("location:liuyanbook.php");//跳回主页
?>

3.class_input.php两个类,验证关键字和验证码的类

<?php
class input {function post($word) {if ($word == "") {return false;}//定义禁止使用的用户$name = ["李星","王瑶"];//循环禁止使用的关键字,user一一与它对比foreach ($name as $key => $value) {if ($word == $value) {return false;}}return true;}
}class codeinput {function check($code) {if ($code == "") {return false;}//判断输入验证码是否正确if (isset($code)) {session_start();if (strtolower($code) == $_SESSION["authcode"]) {return true;} else {return false;}exit();
}   }
}?>

4.dbcontent.php数据库连接

<?php
$host = "127.0.0.1";
$dbuser = "root";
$password = "";
$dbname = "php10";$db = new mysqli($host, $dbuser, $password, $dbname);
if ($db->connect_errno != 0) {die("连接数据库失败!");
}
$db->query("set names UTF8");
?>

5.str.php生成验证码

<?phpsession_start();header("content-type: image/png");//生成白色底图$image = imagecreatetruecolor(100, 30);$bgcolor = imagecolorallocate($image, 255, 255, 255);imagefill($image, 0, 0, $bgcolor);//在白色底图上生成4个彩色随机字符/*1.生成4个数字for($i=0;$i<4;$i++) {$fontsize = 6;$fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));$fontcontent = rand(0, 9);$x = ($i*100/4) + rand(5, 10);$y = rand(5, 10);imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);}*///2.生成随机字母数字$captch_code = "";//将生成的验证码存入该变量中for($i=0;$i<4;$i++) {$fontsize = 6;$fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));//随机截取字母数字$data = "abcdefghijkmnopqrstuvwxyz23456789";$fontcontent = substr($data,rand(0,strlen($data)),1);$captch_code.= $fontcontent;//追加到验证码存放$x = ($i*100/4) + rand(5, 10);$y = rand(5, 10);imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);}$_SESSION["authcode"] = $captch_code;//保存到session中//在白色底图上生成随机点(干扰元素)for($i=0;$i<200;$i++) {$pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));imagesetpixel($image, rand(1, 29), rand(1, 29), $pointcolor);}//在白色底图上生成随机线(干扰元素)for($i=0;$i<3;$i++) {$linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1, 29), $linecolor);}imagepng($image);imagedestroy($image);?>

6.style.css界面样式

.all {width: 800px;margin: 0px auto;
}.contentout {width: 1000px;margin: 0 auto;}.date {text-align:right;
}
.h {text-align:center;
}
.add {overflow: hidden;}
.add .content {width: 98%;padding: 6px;margin: 0px;font-size:20px;}.add .btn {float: right;width: 100px;height: 40px;font-size: 20px;
}.msg {margin: 10px 0px;background: #ccc;padding: 10px;font-size: 18px;
}
.msg .info{overflow: hidden;}
.msg .user {float: left;color: blue;
}.msg .num { font-style: italic; font-size: 16px;color: red;
}
.msg .time {float: right;color: #999;
}
.msg .content {width: 100%;
}
.page {text-align:center;font-size: 18px;
}`
`.all {width: 800px;margin: 0px auto;
}.contentout {width: 1000px;margin: 0 auto;}.date {text-align:right;
}
.h {text-align:center;
}
.add {overflow: hidden;}
.add .content {width: 98%;padding: 6px;margin: 0px;font-size:20px;}.add .btn {float: right;width: 100px;height: 40px;font-size: 20px;
}.msg {margin: 10px 0px;background: #ccc;padding: 10px;font-size: 18px;
}
.msg .info{overflow: hidden;}
.msg .user {float: left;color: blue;
}.msg .num { font-style: italic; font-size: 16px;color: red;
}
.msg .time {float: right;color: #999;
}
.msg .content {width: 100%;
}
.page {text-align:center;font-size: 18px;
}

三.数据库
表结构

四.截图界面

转载于:https://blog.51cto.com/anfishr/2141316

PHP初级练习实战之公司留言板(原生)相关推荐

  1. Kubernetes实战实现 Guestbook 留言板-简易版详解

    1.基础信息 系统版本:CentOS Linux release 7.6 kubernetes版本:kubernetes1.14.0 Docker版本: Docker CE 19.03.5 此 Jav ...

  2. 前端学习(1645):前端系列实战课程之留言板功能实现

  3. android中留言板功能,js 实现简易留言板功能

    无标题文档 li{list-style:none;} li{position:relative;width:500px;} a{position:absolute;right:10px;} var c ...

  4. Vue实战篇二十九:模拟一个简易留言板

    系列文章目录 Vue基础篇一:编写第一个Vue程序 Vue基础篇二:Vue组件的核心概念 Vue基础篇三:Vue的计算属性与侦听器 Vue基础篇四:Vue的生命周期(秒杀案例实战) Vue基础篇五:V ...

  5. 机器学习实战4.2 朴素贝叶斯案例:屏蔽社区留言板的侮辱性言论

    机器学习实战4.2 朴素贝叶斯案例:屏蔽社区留言板的侮辱性言论 参考地址:https://cuijiahua.com/blog/2017/11/ml_4_bayes_1.html 一.引子 很久没更新 ...

  6. React实战:留言板

    留言板 一.展示 留言板功能:"增加留言","删除留言","修改留言" 二.组件设计 CommentBox是顶层组件,里面包含 Commen ...

  7. Django框架简介-初级(简单留言板)

    Django框架(综合各方知识) 介绍Django和设计模式 CGI方式开发web MVC 设计模式 web各部分说明 主要内容 结构说明 必要步骤 简单体验留言板 进阶留言板:css和html分离, ...

  8. Thinkphp5实战之留言板

    本节主要通过Thinkphp5来实现简易留言板的基本功能.麻雀虽小,五脏俱全.从以下几个方面来进一步认识Thinkphp5,包括:目录布局.MCV的运用.对数据的增删改查.留言板功能逻辑等.打牢基础, ...

  9. python+Mysql+flask架构的在线留言板实战

    在线留言板实战项目说明文档 用python操作mysql完成一个在线留言板系统 1.设计留言板表 2.通过python语言实现 1 项目基本功能 留言板系统主要功能:(要求使用类实现) 1.显示留言板 ...

最新文章

  1. 最佳页面置换算法代码_(存储管理)页面置换算法
  2. ECSHOP删除云服务
  3. 0603贴片电阻阻值对照表_怎样读贴片电阻阻值
  4. Eclipse报错:!!MESSAGE Job found still running.......
  5. 阿里大牛精心整理了46张PPT,教你弄懂JVM、GC算法和性能调优!
  6. 字节(bytes) 二进制序列类型
  7. 按照c语言规首字母只能是,C语言--指针 - osc_nbqoh20k的个人空间 - OSCHINA - 中文开源技术交流社区...
  8. CICD详解(八)——gitlab安装与配置
  9. python登录微信pc版_微信PC版内测更新,又增加2个实用功能
  10. 如何快速设计一款万能遥控器产品原型(SoC免开发)
  11. 2022年基于PXI/PCI/PCIe/USB总线的高速数据采集卡汇总
  12. 双非本科生进大厂,而我还在底层默默地爬树(上)
  13. java重定向url_Java根据新的URL 对网页进行重定向
  14. idea 如何使用tomcat启动项目
  15. 盘点,Windows上提高效率的软件
  16. Vuex简介(带操作实例)
  17. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: Certificate signatur
  18. 前端web(网络)相关概念(含经典面试题不多)
  19. 解决帝国时代 之 罗马复兴 不能加入游戏的问题
  20. tomcat无法成功打开,因为设置catalina_home的时候在最后加了“;”符号

热门文章

  1. python关机怎样保存seek_在Python中操作文件之seek()方法的使用教程
  2. 与计算机运算速度相关参数,计算机CPU运算速度是多少
  3. centos java jdk 升级_centos jdk 升级到1.7.0_45方法
  4. 基于信息熵确立权重的topsis法_基于信息熵和TOPSIS法的装备战场抢修排序决策模型...
  5. HarmonyOS竞赛,2021全国大学生物联网设计竞赛正式开赛,全新HarmonyOS赛题引关注...
  6. joomla 3.6 mysql 版本_Joomla是否支持MariaDB数据库?
  7. qgis 图片_QGIS教程09QGIS中如何制作萤火虫地图?
  8. python用程序说爱你_用python写一个聊天小程序!和女朋友的专属聊天工具!
  9. zTree动态添加节点
  10. Linux编译和下载嵌入式实验,嵌入式实验6交叉编译及Linux简单程序设计实验