PHP Primary practice of the message board
Beginners do things, some places do not write well, haha ha!
I. Knowledge Focus
1. Three mesh operation $page = Empty ($_get[' P '])? 1: $_get[' P '];
2. How the database operates MYSQLI
3.html CSS JS
4. Concatenation of strings
5. Making pagination Page numbers
6. Production of complete verification code
7. Use of Classes
8.session method of passing verification code
Two. Code
Code divided into 6 files, liuyanbook.php for home page
1.liuyanbook.php Main interface
<?php//date $weekday = ["Day", "one", "two", "three", "four", "five", "six"];d ate_default_timezone_set (' Asia/shanghai '); $now = getdate (Time ()), $week = $now [' wday ']; $date = date ("Y year M month D Day week $weekday[$week]")//echo $date;//Set page number variable//default home p=1$ page= empty ($_get[' P '])? 1: $_get[' P '];//page $showpage = 3;//calculate offset $pageoffset = ($showPage-1)/2;//Connect to database, fetch data $host = "127.0.0.1"; $dbuser = "root"; $ Password = ""; $dbname = "PhP10"; $db = new Mysqli ($host, $dbuser, $password, $dbname); if ($db->connect_errno! = 0) { echo "Connection failed:"; Echo $db->connect_error; Exit;} Paging $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 Error!) "; Exit;} Var_dump ($mysqli _result);//Total pages $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;//Count pages $total _pages = ceil ($tOTAL/5);//echo $total _pages;? ><! DOCTYPE html>
2.liuyan.php back-end validation, inserting data
<?phpinclude(‘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 class of two classes, validating keywords and captcha
<?phpclass 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 Database Connection
<?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 Generate verification Code
<?phpsession_start (); Header ("Content-type:image/png"); Generate a white basemap $image = imagecreatetruecolor (100, 30); $bgcolor = Imagecolorallocate ($image, 255, 255, 255); Imagefill ($image, 0, 0, $bgcolor); Generates 4 colored random characters/*1 on a white basemap. Generate 4 digits for ($i =0; $i <4; $i + +) {$fontsize = 6; $fontcolor = Imagecolorallocate ($image, rand (0, +), rand (0, +), 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. Generates a random alphanumeric $captch _code = "";//The generated verification code is stored in the variable for ($i =0; $i <4; $i + +) {$fontsize = 6; $fontcolor = Imagecolorallocate ($image, rand (0, +), rand (0, +), rand (0, 120)); Random interception of alphanumeric $data = "abcdefghijkmnopqrstuvwxyz23456789"; $fontcontent = substr ($data, Rand (0,strlen ($DATA)), 1); $captch _code.= $fontcontent;//Append to verification code to store $x = ($i *100/4) + rand (5, 10); $y = rand (5, 10); Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } $_session["Authcode"] = $captch _code;//Save to SESSION//generate random points on white basemap (interference Element) for ($i =0; $i <200; $i + +) {$point color = Imagecolorallocate ($image, rand (+), rand (50, 200)); Imagesetpixel ($image, rand (1,), rand (1,), $pointcolor); }//Generate random lines (interference elements) for ($i =0; $i <3; $i + +) on white basemaps {$linecolor = Imagecolorallocate ($image, Rand (UP), Rand (80 , (+), rand (80, 220)); Imageline ($image, rand (1), Rand (1, $), rand (1), rand (1), $linecolor); } imagepng ($image); Imagedestroy ($image);? >
6.STYLE.CSS interface Style
. 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;}
Three. Database
Table structure
Four. Interface
PHP Primary practice of the company Message board (native)