Message Display page: added the paging and message reply functions compared to the previous example.
Main Code:
Install/index. php: Program Installation page
Copy codeThe Code is as follows:
<? Php
If ($ _ GET ["action"]! = 1)
{
?>
<Form method = "post" action = "index. php? Action = 1 ">
<Table border = "1">
<Tr>
<Td> MySQL Host Name: </td>
<Td> <input type = "text" name = "host" value = "localhost"> </td>
</Tr>
<Tr>
<Td> MySQL Username: </td>
<Td> <input type = "text" name = "user" value = "root"> </td>
</Tr>
<Tr>
<Td> MySQL password: </td>
<Td> <input type = "password" name = "password" value = "> </td>
</Tr>
<Tr>
<Td> Database Name: </td>
<Td> <input type = "text" name = "database" value = "ruizhi_messageboard"> </td>
</Tr>
<Tr>
<Td> Number of messages on each page: </td>
<Td> <input type = "text" name = "pagenum" value = "10"> </td>
</Tr>
<Tr>
<Td> Administrator Account: </td>
<Td> <input type = "text" name = "admin_name" value = "admin"> </td>
</Tr>
<Tr>
<Td> administrator password: </td>
<Td> <input type = "text" name = "admin_password" value = "admin"> </td>
</Tr>
<Tr>
<Td> message topic: </td>
<Td> <input type = "text" name = "boardname" value = "RUIZHINET message book"> </td>
</Tr>
<Tr>
<Td colspan = "2" align = "center"> <input type = "submit" name = "submit" value = "Confirm installation"> <input type = "button" name = "submit" value = "Close page" onclick = "window. close () "> </td>
<Td> </td>
</Tr>
</Table>
</Form>
<? Php
} Else {
@ Set_time_limit (1000 );
$ Lockfile = "install. lock ";
$ Host = "localhost ";
$ User = "root ";
$ Password = "";
$ Database = "ruizhi_messageboard ";
$ Pagenum = 10;
$ Admin_name = "admin ";
$ Admin_password = "123456 ";
$ Boardname = "RUIZHINET message book ";
If ($ _ POST ["host"]! = ""){
$ Host = $ _ POST ["host"];
}
If ($ _ POST ["user"]! = ""){
$ User = $ _ POST ["user"];
}
If ($ _ POST ["password"]! = ""){
$ Password = $ _ POST ["password"];
}
If ($ _ POST ["database"]! = ""){
$ Database = $ _ POST ["database"];
}
If ($ _ POST ["pagenum"]! = ""){
$ Pagenum = $ _ POST ["pagenum"];
}
If ($ _ POST ["admin_name"]! = ""){
$ Admin_name = $ _ POST ["admin_name"];
}
If ($ _ POST ["admin_password"]! = ""){
$ Admin_password = $ _ POST ["admin_password"];
}
If ($ _ POST ["boardname"]! = ""){
$ Boardname = $ _ POST ["boardname"];
}
If (file_exists ($ lockfile )){
Exit ("already installed. If you want to reinstall it, delete install/install. lock ");
}
$ Conn = mysql_connect ($ host, $ user, $ password );
If ($ conn ){
$ SQL _drop_database = "DROP DATABASE IF EXISTS '". $ database ."'";
$ SQL _create_database = "CREATE database'". $ DATABASE ."'";
$ SQL _create_table_messageboard = "CREATE TABLE 'messageboard '(
'Id' int (11) not null auto_increment,
'Author' varchar (15) not null,
'Title' varchar (30) not null,
'Smiles 'varchar (30) not null,
'Content' mediumtext,
'Photo' varchar (30) not null,
'Addtime' int (11) default NULL,
Primary key ('id ')
) ENGINE = MyISAM default charset = gbk AUTO_INCREMENT = 1 ";
$ SQL _create_table_reply = "CREATE TABLE 'reply '(
'Id' int (11) not null auto_increment,
'Msgid' int (11) not null,
'Content' mediumtext,
'Addtime' int (11) default NULL,
Primary key ('id ')
) ENGINE = MyISAM default charset = gbk AUTO_INCREMENT = 1 ";
If (mysql_query ($ SQL _drop_database, $ conn )){
If (mysql_query ($ SQL _create_database, $ conn )){
Mysql_select_db ($ database, $ conn );
If (mysql_query ($ SQL _create_table_messageboard, $ conn) & mysql_query ($ SQL _create_table_reply, $ conn )){
$ Config_file = "../config. php ";
$ Config_strings = "<? Php \ n ";
$ Config_strings. = "\ $ boardname = \" ". $ boardname." \ "; \ n ";
$ Config_strings. = "\ $ host = \" ". $ host." \ "; \ n ";
$ Config_strings. = "\ $ user = \" ". $ user." \ "; \ n ";
$ Config_strings. = "\ $ password = \" ". $ password." \ "; \ n ";
$ Config_strings. = "\ $ database = \" ". $ database." \ "; \ n ";
$ Config_strings. = "\ $ pagenum = \" ". $ pagenum." \ "; \ n ";
$ Config_strings. = "\ $ admin_name = \" ". $ admin_name." \ "; \ n ";
$ Config_strings. = "\ $ admin_password = \" ". $ admin_password." \ "; \ n ";
$ Config_strings. = "\ $ conn = mysql_connect (\ $ host, \ $ user, \ $ password); \ n ";
$ Config_strings. = "mysql_select_db (\ $ database, \ $ conn); \ n ";
$ Config_strings. = "?> ";
If ($ fp = fopen ($ config_file, "wb ")){
If (fwrite ($ fp, $ config_strings )){
If ($ fp2 = fopen ($ lockfile, 'w '))
{
Fwrite ($ fp2, '20140901 ');
Fclose ($ fp2 );
}
Echo "installation successful! The configuration file is config. php. You can manually modify the file ";
Echo "\ n <a href = '../index. php'> go to the system homepage </a> ";
} Else {
Exit ("file write failed ");
}
Fclose ($ fp );
}
} Else {
Exit ("the create table statement cannot be executed:". $ SQL _create_table );
}
} Else {
Exit ("the create database statement cannot be executed:". $ SQL _create_database );
}
} Else {
Exit ("the drop database statement cannot be executed:". $ SQL _drop_database );
}
} Else {
Exit ("failed to connect to the database. Check the MySQL host name, user name, and password ");
}
}
The generated config. php file:
Copy codeThe Code is as follows:
<? Php
$ Boardname = "RUIZHINET message book ";
$ Host = "localhost ";
$ User = "root ";
$ Password = "";
$ Database = "ruizhi_messageboard ";
$ Pagenum = "10 ";
$ Admin_name = "admin ";
$ Admin_password = "admin ";
$ Conn = mysql_connect ($ host, $ user, $ password );
Mysql_select_db ($ database, $ conn );
?>
Index. php: Program display page
Copy codeThe Code is as follows:
<? Php
If (! $ _ GET ["page"]) {
$ Page = 1;
} Else {
$ Page = $ _ GET ["page"];
}
$ SQL = "SELECT id FROM messageboard ";
$ Result = mysql_query ($ SQL, $ conn );
$ Row_num = mysql_num_rows ($ result );
$ Page_count = ceil ($ row_num/$ pagenum );
Echo "A total of". $ row_num. "messages ";
Echo "divided into". $ page_count. "Page display ";
Echo "current". $ page. "page ";
?>
<P>
<? Php
If ($ row_num = 0 ){
Echo "no message ";
} Else {
?>
<Table border = "1" width = "700">
<? Php
$ Offset = ($ page-1) * $ pagenum;
$ Sql2 = "SELECT * FROM messageboard order by id desc limit $ offset, $ pagenum ";
$ Result2 = mysql_query ($ sql2, $ conn );
While ($ row = mysql_fetch_array ($ result2 )){
$ Offset ++;
Echo "<tr> ";
Echo "<td align = left bgcolor = # CCCCFF> ";
Echo $ offset. ". ";
Echo base64_decode ($ row [title]). "-". base64_decode ($ row [author]). "[". date ("Y, m, D", $ row [addtime]). "]";
If (isset ($ _ SESSION ["admin_name"]) & $ _ SESSION ["admin_name"]! = ""){
Echo "<a href = del. php? Id = ". $ row [" id "]."> Delete | </a> ";
Echo "<a href = reply. php? Id = ". $ row [" id "]."> reply </a> ";
}
Echo "</td> </tr> ";
Echo "<tr> <td align = left>". base64_decode ($ row ["content"]). "</td> </tr> ";
If ($ row ["photo"]! = "NONE ")
{
Echo "<tr> <td align = left> </td> </tr> ";
}
$ Sql3 = "SELECT * FROM reply WHERE msgid = $ row [id] order by id DESC ";
$ Result3 = mysql_query ($ sql3, $ conn );
While ($ row3 = mysql_fetch_array ($ result3 )){
Echo "<tr> <td align = left bgcolor = #99 CCFF> reply:". date ("Y, m, D", $ row3 [addtime]);
If (isset ($ _ SESSION ["admin_name"]) & $ _ SESSION ["admin_name"]! = ""){
Echo "<a href = delreply. php? Id = ". $ row3 [" id "]."> Delete </a> ";
}
Echo "</td> </tr> ";
Echo "<tr> <td align = left>". base64_decode ($ row3 [content]). "</td> </tr> ";
}
}
?>
<? Php
If ($ page_count> 1 ){
$ Prev_page = $ page-1;
$ Next_page = $ page + 1;
If ($ page <= 1 ){
Echo "Page 1 | ";
} Else {
Echo "<a href = 'index. php? Page = 1'> page 1 </a> | ";
}
If ($ prev_page <1 ){
Echo "Previous Page | ";
} Else {
Echo "<a href = 'index. php? Page = ". $ prev_page." '> previous page </a> | ";
}
If ($ next_page> $ page_count ){
Echo "next page | ";
} Else {
Echo "<a href = 'index. php? Page = ". $ next_page." '> next page </a> | ";
}
If ($ page >=$ page_count ){
Echo "last page ";
} Else {
Echo "<a href = 'index. php? Page = ". $ page_count." '> last page </a> ";
}
}
?>
</Table>
<? Php
}
?>
<? Php
If (isset ($ _ SESSION ["admin_name"]) & $ _ SESSION ["admin_name"]! = ""){
?>
<P> <a href = "logout. php"> exit management </a> </p>
<? Php
} Else {
?>
<P> <a href = "login. php"> logon management </a> </p>
<? Php
}
?>
Saveadd. php: Save the message
Copy codeThe Code is as follows:
<? Php
If (! $ _ POST ["author"] |! $ _ POST ["content"])
{
Echo "<meta http-equiv = \" refresh \ "content = \" 2; url = index. php \ "> \ n ";
Echo "You did not fill in the message name or content, 2 seconds back to the home page ";
Exit ();
} Else {
$ Imgflag = 0;
Function random ($ length)
{
$ Hash = 'img -';
$ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
$ Max = strlen ($ chars)-1;
Echo (double) microtime () * 1000000;
For ($ I = 0; $ I <$ length; $ I ++)
{
$ Hash. = $ chars [mt_rand (0, $ max)];
}
Return $ hash;
}
Function fileext ($ filename)
{
Return substr (strrchr ($ filename, '.'), 1 );
}
If ($ _ FILES ["upfile"] ["name"]! = ""){
$ Uploaddir = "upfile /";
$ Type = array ("jpg", "gif", "bmp", "jpeg", "png ");
If (! In_array (strtolower (fileext ($ _ FILES ['upfile'] ['name']), $ type ))
{
Echo "<meta http-equiv = \" refresh \ "content = \" 2; url = index. php \ "> \ n ";
$ Text = implode (",", $ type );
Echo "You can only upload the following types of files:", $ text, "<br> ";
Exit ();
}
Else
{
$ Filename = explode (".", $ _ FILES ['upfile'] ['name']);
Do
{
$ Filename [0] = random (10 );
$ Randname = implode (".", $ filename );
$ Uploadfile = $ uploaddir. $ randname;
} While (file_exists ($ uploadfile ));
If (move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ uploadfile )){
Echo "image uploaded ";
$ Imgflag = 1;
}
Else {
Echo "An error occurred while uploading the image! ";
$ Imgflag = 0;
}
}
}
$ Author = base64_encode ($ _ POST ["author"]);
$ Content = base64_encode (ereg_replace ("\ r \ n", "<br>", htmlspecialchars ($ _ POST ["content"]);
$ Smiles = base64_encode ($ _ POST ["smiles"]);
If ($ _ POST ["title"]) {
$ Title = base64_encode ($ _ POST ["title"]);
} Else {
$ Title = base64_encode ("No title ");
}
$ Addtime = time ();
If ($ imgflag = 1 ){
$ Photo = base64_encode ($ randname );
} Else {
$ Photo = "NONE ";
}
// Save data
$ SQL = "INSERT INTO messageboard (author, title, smiles, content, photo, addtime) VALUES ('$ author',' $ title', '$ smiles ', '$ content',' $ photo ', $ addtime )";
Mysql_query ($ SQL, $ conn );
Echo "<meta http-equiv = \" refresh \ "content = \" 2; url = index. php \ "> \ n ";
Echo "thank you for your message. Return to the homepage in 2 seconds ";
}
?>
Savereply. php: Save the reply
<? Php
If (isset ($ _ SESSION ["admin_name"]) & $ _ SESSION ["admin_name"]! = "")
{
If (! $ _ POST ["content"])
{
Echo "<meta http-equiv = \" refresh \ "content = \" 2; url = index. php \ "> \ n ";
Echo "You did not fill in the reply content, 2 seconds back to the home page ";
Exit ();
} Else {
$ Msgid = $ _ POST ["msgid"];
$ Content = base64_encode (ereg_replace ("\ r \ n", "<br>", htmlspecialchars ($ _ POST ["content"]);
$ Addtime = time ();
// Save data
$ SQL = "INSERT INTO reply (msgid, content, addtime) VALUES ($ msgid, '$ content', $ addtime )";
Mysql_query ($ SQL, $ conn );
Echo "<meta http-equiv = \" refresh \ "content = \" 2; url = index. php \ "> \ n ";
Echo "thank you for your message. Return to the homepage in 2 seconds ";
}
}
?>