Php simple message board program code

Source: Internet
Author: User
Tags echo date strlen

 

The code is as follows: Copy code

<? Php
Session_start ();
$ Con = mysql_connect ('localhost', 'root', 'root') or die ('database connection failed! ');
Mysql_query ('set names utf8 ');
Mysql_select_db ('guestbook ');

$ Pagesize = 10; // The number of message records displayed on each page
If (isset ($ _ GET ['Page']) & $ _ GET ['Page']! = '') $ Page = $ _ GET ['Page'];
Else $ page = 0;

$ SQL = "SELECT a. *, B. name, B. email, B. qq, c. revert_time, c. revert
FROM post
Left join revert c ON (a. id = c. post_id), guest B
WHERE a. guest_id = B. id
Order by a. id DESC ";
$ NumRecord = mysql_num_rows (mysql_query ($ SQL ));
$ Totalpage = ceil ($ numRecord/$ pagesize );

$ RecordSql = $ SQL. "LIMIT". $ page * $ pagesize. ",". $ pagesize;
$ Result = mysql_query ($ recordSql );
?>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>
<Title> PHPiask simple message board </title>
<Style type = "text/css">
<! --
Body {
Margin-left: 0px;
Margin-top: 0px;
}
A: link {
Text-decoration: none;
Color: # FF6600;
}
A: visited {
Text-decoration: none;
}
A: hover {
Text-decoration: underline;
}
A: active {
Text-decoration: none;
}
. STYLE1 {
Color: # FFFFFF;
Font-weight: bold;
Font-size: 16px;
}
Td {
Font-size: 12px;
}
. Tdhx {
Font-style: italic;
Line-height: 1.5;
Text-decoration: underline;
}
-->
</Style>
<Script language = "javascript">
Function checkInput (){
Var Email = document. getElementById ('email ');
Var QQ = document. getElementById ('QQ ');
Var name = document. getElementById ('name ');
Var post = document. getElementById ('post ');
// Authentication username: cannot exceed 10 characters (5 Chinese characters), cannot enter invalid characters, cannot be blank
NameValue = name. value. replace (/s +/g ,"");
Var SPECIAL_STR = "~! % ^ & * (); "?> <[] {}\|,:/= + -";
Var nameflag = true;
For (I = 0; I <nameValue. lenght; I ++ ){
If (SPECIAL_STR.indexOf (nameValue. charAt (I ))! =-1)
Nameflag = false;
 }
If (nameValue = ''){
Alert ('enter the user name! ');
Return false;
 }
If (nameValue. length> 10 ){
Alert ('The user name can contain up to 10 characters (5 Chinese characters )! ');
Return false;
 }
 
If (nameflag === false ){
Alert ('user name cannot contain invalid characters. Please change it! ');
Return false;
 }
// Verify the QQ number
Var par =/^ [1-9] d {4, 12} $ /;
If (QQ. value! = ''&&! Par. test (QQ. value )){
Alert ('Enter the correct QQ number ');
Return false;
 }
// Verify the Email address
Var emailpar =/^ [w-] + (. [w-] +) * @ [w-] + (. [w-] +) + $ /;
If (Email. value! = ''&&! Emailpar. test (Email. value )){
Alert ('Enter the correct email address! ');
Return false;
 }
If (QQ. value = ''& Email. value = ''){
Alert ('mailbox and QQ are mandatory ');
Return false;
 }
If (post. value = ""){
Alert ('Enter the message content! ');
Return false;
 }
If (post. value. length> 400 ){
Alert ('the message content is too long! ');
Return false;
 }

}
</Script>
</Head>

<Body>
<Table width = "800" border = "0" align = "center">

<Tr>
<Td height = "80" bgcolor = "#003366"> <span class = "STYLE1"> simple message board tutorial (<a href = "http://www.phpiask.com"> PHP iask </a>) </span> </td>
</Tr>
<Tr>
<Td height = "5" bgcolor = "# efefef"> </td>
</Tr>
</Table>
<Table width = "800" border = "0" align = "center" bgcolor = "# fefefe">
<? Php
While ($ rs = mysql_fetch_object ($ result )){
?>
<Tr>
<Td class = "tdhx"> Contact: <? Php echo $ rs-> name?> | Email: <? Php echo $ rs-> email?> | QQ: <? Php echo $ rs-> qq?> | Message Time: <? Php echo date ("Y-m-d H: I: s", $ rs-> post_time + 8*3600)?> </Td>
</Tr>
<? Php
If (isset ($ _ SESSION ['login']) & $ _ SESSION ['login']) {
?>
<Tr>
<Td class = "tdhx"> <a href = "revert. php? Id = <? Php echo $ rs-> id?> "> Reply </a> | <a href =" delete. php? Id = <? Php echo $ rs-> id?> "> Delete </a> </td>
</Tr>
<? Php
  }
?>
<Tr>
<Td> message content: <? Php echo nl2br (htmlspecialchars ($ rs-> post)?> <Br/>
<Font color = "Red">
Reply content: <? Php echo nl2br (htmlspecialchars ($ rs-> revert)?> [<? Php if ($ rs-> revert_time! = "") Echo date ("Y-m-d H: I: s", $ rs-> revert_time + 8*3600)?> ]
</Font>
   
</Td>
</Tr>
<Tr> <td height = "3px" bgcolor = "# FF6600"> </td> </tr>
<? Php
}
?>
</Table>
<Table width = "800" border = "0" align = "center" bgcolor = "# B1C3D9">
<Tr>
<Td>
<? Php
If ($ page> 0) echo "<a href = 'index. php? Page = ". ($ page-1)." '> Previous page | </a> ";
If ($ page <$ totalpage-1) echo "<a href = 'index. php? Page = ". ($ page + 1)." '> Next page </a> ";
?> </Td>
</Tr>
</Table> <form action = "post. php" method = "post" id = "postForm" name = "postForm">
<Table width = "800" border = "0" align = "center" cellspacing = "1" bgcolor = "# efefef">
 
<Tr>
<Td width = "117" bgcolor = "# FFFFFF"> Name: </td>
<Td width = "673" bgcolor = "# FFFFFF"> <label>
<Input type = "text" name = "name" id = "name"/>
</Label> </td>
</Tr>
<Tr>
<Td bgcolor = "# FFFFFF"> Email: </td>
<Td bgcolor = "# FFFFFF"> <label>
<Input type = "text" name = "email" id = "email"/>
</Label> </td>
</Tr>
<Tr>
<Td bgcolor = "# FFFFFF"> QQ: </td>
<Td bgcolor = "# FFFFFF"> <label>
<Input type = "text" name = "qq" id = "qq"/>
</Label> </td>
</Tr>
<Tr>
<Td colspan = "2" bgcolor = "# FFFFFF"> Message Content: </td>
</Tr>
<Tr>
<Td colspan = "2" bgcolor = "# FFFFFF"> <label>
<Textarea name = "post" id = "post" cols = "40" rows = "5"> </textarea>
</Label> </td>
</Tr>
<Tr>
<Td colspan = "2" bgcolor = "# FFFFFF"> <label>
<Input type = "submit" name = "Submit" value = "submit" onclick = "return checkInput ();"/>
& Nbsp;
<Input type = "reset" name = "Submit2" value = "reset"/>
</Label> <a href = "login. php"> administrator logon </a> </td>
</Tr>
</Table> </form>
</Body>
</Html>

Post. Php file

<? Php
Header ('content-type: text/html; charset = utf-8 ');
// Escape these values if the PHP automatic escape function is not enabled
If (! Get_magic_quotes_gpc ()){
Foreach ($ _ POST as & $ items ){
$ Items = addslashes ($ items );
 }
}

$ Name = $ _ POST ['name'];
$ Qq =$ _ POST ['QQ'];
$ Email = $ _ POST ['email '];
$ Post = $ _ POST ['post'];

If ($ name = "" | strlen ($ name)> 10 ){
Echo <tem
<Script language = "javascript">
Alert ('Enter the correct account name ');
History. go (-1 );
</Script>
Tem;
Exit ();
}
If ($ qq = "" & $ email = ""){
Echo <tem
<Script>
Alert (either 'email or QQ is required! ');
History. go (-1 );
</Script>
Tem;
Exit ();
}
If ($ qq! = ""&&(! Is_numeric ($ qq) | $ qq> 9999999999 | $ qq <= 9999 )){
Echo <tem
<Script>
Alert ("Enter the correct QQ number ");
History. go (-1 );
</Script>
Tem;
Exit ();
}
If ($ email! = ""&&(! Ereg ("^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (. [a-zA-Z0-9 _-]) + ", $ email) | strlen ($ email)> 60 )){
Echo <tem
<Script>
Alert ("Enter the correct Email ");
History. go (-1 );
</Script>
Tem;
Exit ();
}
If (strlen ($ post)> 400 ){
Echo <tem
<Script>
Alert ("The entered message is too long! ");
History. go (-1 );
</Script>
Tem;
Exit ();
}

// Link to the database
$ Con = mysql_connect ('localhost', 'root', 'root') or die ('database connection failed! ');
Mysql_query ('set names utf8 ');
Mysql_select_db ('guestbook ');

// Insert customer information into the guest table
$ InsertSql = "insert into guest (name, qq, email) values ('$ name',' $ QQ', '$ email ')";
If (mysql_query ($ insertSql )){
$ Guestid = mysql_insert_id ();
}
Else {
Echo $ insertSql;
Echo mysql_error ();
Echo "data insertion failed! ";
Exit ();
}

// Insert the customer id and message obtained after the above inserts into the post table
$ Post_time = time ();
$ InsertPostSql = "insert into post (guest_id, post, post_time) values ('$ guestid', '$ post',' $ post_time ')";
If (mysql_query ($ insertPostSql )){
Echo <tem
<Script>
Alert ("message succeeded ");
Location. href = "index. php ";
</Script>
Tem;
}
Else {
Echo <tem
<Script>
Alert ("Message failed ");
Location. href = "index. php ";
</Script>
Tem;
}
?>

The following is the login. php logon first page of the background management page.

 

The code is as follows: Copy code
<? Php
Session_start ();
If (isset ($ _ POST ['submit ']) {
If (! Get_magic_quotes_gpc ()){
Foreach ($ _ POST as & $ items ){
$ Items = addslashes ($ items );
  }
 }
If ($ _ POST ['username'] = 'phpask '& md5 ($ _ POST ['password']) = '6dc88b87062a5de19895e952fa290dad '){
$ _ SESSION ['login'] = true;
Echo "<script> alert ('Administrator logon succeeded '); location. href = 'index. Php'; </script> ";
Exit ();
 }
Else {
Echo "<script> alert ('logon failed! '); </Script> ";
 }
}
?>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>
<Title> Untitled Document </title>
</Head>
<Body>
<Table>
<Tr>
<Td>
<Form action = "login. php" method = "POST" name = "form1">
Username: <input type = "text" name = "username" size = "20"/>
Password: <input type = "password" name = "password" size = "20">
<Input type = "submit" value = "login" name = "Submit"/>
<Input type = "button" onclick = "javascript: location. href = 'index. Php'" value = "discard"/>
</Form>
</Td>
</Tr>
</Table>
</Body>
</Html>

Delete. php

The code is as follows: Copy code

<? Php
Session_start ();
Header ('content-type: text/html; charset = utf-8 ');
$ Con = mysql_connect ('localhost', 'root', 'root') or die ('database connection failed! ');
Mysql_query ('set names utf8 ');
Mysql_select_db ('guestbook ');

If (! $ _ SESSION ['login']) {
Echo "<script> alert ('you are not authorized! '); Location. href = 'index. Php'; </script> ";
Exit ();
}

If (isset ($ _ GET ['id']) & $ _ GET ['id']! = ""){
$ DelRevertSql = "delete from revert where post_id =". $ _ GET ['id'];
Mysql_query ($ delRevertSql );
 
$ DelGuestSql = "delete from guest where id = (select guest_id from post where id =". $ _ GET ['id']. ")";
Mysql_query ($ delGuestSql );
 
$ DelPostSql = "delete from post where id =". $ _ GET ['id'];
Mysql_query ($ delPostSql );
 
If (mysql_error () = ""){
Echo "<script> alert ('deleted successfully! '); Location. href = 'index. Php'; </script> ";
 }
}
?>

Reply to the revert. Php file

The code is as follows: Copy code

<? Php
Session_start ();
$ Con = mysql_connect ('localhost', 'root', 'root') or die ('database connection failed! ');
Mysql_query ('set names utf8 ');
Mysql_select_db ('guestbook ');

If (! $ _ SESSION ['login']) {
Echo "<script> alert ('no logon, cannot reply! '); Location. href = 'index. Php'; </script> ";
Exit ();
}
If ($ _ POST ['submit ']) {
If (! Get_magic_quotes_gpc ()){
Foreach ($ _ POST as $ items ){
$ Items = addslashes ($ items );
  }
 }
If (strlen ($ _ POST ['revert'])> 400 ){
Echo "<script> alert ('The reply content is too long! '); History. go (-1); </script> ";
Exit ();
 }
$ Post_id = $ _ POST ['post _ id'];
$ Revert = $ _ POST ['revert'];
$ InsertRevertSql = "insert into revert (post_id, revert, revert_time) value ('$ post_id', '$ revert', '$ time ')";
If (mysql_query ($ insertRevertSql )){
Echo "<script> alert ('reply successfully'); location. href = 'index. Php'; </script> ";
Exit ();
 }
Else {
Echo "<script> alert ('reply failed! '); History. go (-1); </script> ";
 }
}
?>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>
<Title> Untitled Document </title>
</Head>
<Body>
<Table>
<Tr>
<Td>
<Form action = "revert. php" method = "POST" name = "form1">
Reply content: <textarea name = "revert" cols = "30" rows = "5" id = "revert"> </textarea>
<Input type = "hidden" name = "post_id" value = "<? Php echo $ _ GET ['id']?> "Size =" 20 ">
<Input type = "submit" value = "reply" name = "Submit"/>
<Input type = "button" onclick = "javascript: history. go (-1);" value = "discard"/>
</Form>
</Td>
</Tr>
</Table>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.