1. Message Display page
2. Publish messages and allow uploading images
3. Enter the password to log on and delete the message.
1. File directory
Upfile is the directory for storing uploaded images.
2. Main Interface
(1) homepage: displays the message page
(2) publish a message page
3. XML document format, named data. xml
The meaning of each field is not much said. The value of each element looks a bit strange because I used base64_encode to encode the string.
4 main page code
(1) add. php
This page is purely HTML code
<FormAction = "saveadd. php" enctype = "multipart/form-data" method = "post"Name = "myform" onsubmit = "return go (this)">
<Table border = "1" width = "600">
<Tr>
<Td> author </td>
<Td align = "left"> <input type = "text" name = "author" size = "10"> </td>
</Tr>
<Tr>
<Td> title </td>
<Td align = "left"> <input type = "text" name = "title" size = "50"> </td>
</Tr>
<Tr>
<Td> emotion </td>
<Td align = "left">
<Select name = "smiles" size = "1" onchange = "change_img ();">
<Option value = "smile.gif"> smile </option>
<Option value = "biggrin.gif"> straight </option>
<Option value = "policy.gif"> victory </option>
<Option value = "tongue.gif"> tongue </option>
<Option value = "titter.gif"> sneer </option>
<Option value = "cry.gif"> cry </option>
<Option value = "curse.gif"> angry </option>
<Option value = "huffy.gif"> angry </option>
<Option value = "mad.gif"> crazy </option>
<Option value = "sad.gif"> grief </option>
<Option value = "shocked.gif"> shocked </option>
<Option value = "shy.gif"> shy </option>
<Option value = "sleepy.gif"> sleepy </option>
<Option value = "sweat.gif"> Khan </option>
</Select>
</Td>
</Tr>
<Tr>
<Td> content </td>
<Td align = "left"> <textarea name = "content" cols = "70" rows = "10"> </textarea> </td>
</Tr>
<Tr>
<Td> </td>
<Td align = "left"> <input type = "file" name = "upfile" size = "50"> </td>
</Tr>
<Tr>
<Td colspan = "2"> <input type = "submit" value = "submit"/> </td>
</Tr>
</Table>
</Form>
(2) savadd. php
Used to save the message
<? 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; // used to determine whether an image needs to be uploaded
Function random ($ length) // This function is used to generate a random Image File Name (excluding the extension) to prevent duplication with existing images.
{
$ Hash = 'img -';
$ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
$ Max = strlen ($ chars)-1;
For ($ I = 0; $ I <$ length; $ I ++) // randomly finds length characters from the preceding string
{
$ Hash. = $ chars [mt_rand (0, $ max)];
}
Return $ hash;
}
Function fileext ($ filename) // This function is used to obtain the extension of the uploaded file.
{
Return substr (strrchr ($ filename, '.'), 1 );
}
If ($ _ FILES ["upfile"] ["name"]! = ""){
$ Uploaddir = "upfile/"; // image storage path
$ Type = array ("jpg", "gif", "bmp", "jpeg", "png"); // specifies the file type that can be uploaded.
If (! In_array (strtolower (fileext ($ _ FILES ['upfile'] ['name']), $ type) // if the extension of the uploaded file does not meet the requirements
{
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); // get the final randomly generated file name (together with the extension)
$ Uploadfile = $ uploaddir. $ randname;
} While (file_exists ($ uploadfile ));
If (move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ uploadfile) {// Save the uploaded image to the upfile folder
Echo "image uploaded ";
$ Imgflag = 1;
}
Else {
Echo "An error occurred while uploading the image! ";
$ Imgflag = 0;
}
}
}
// Obtain other form fields
$ Author = base64_encode ($ _ POST ["author"]);
$ Content = base64_encode (ereg_replace ("\ r \ n", "<br>", $ _ POST ["content"]);
$ Smiles = base64_encode ($ _ POST ["smiles"]);
If ($ _ POST ["title"]) {
$ Title = base64_encode ($ _ POST ["title"]);
} Else {
$ Title = base64_encode ("No title ");
}
$ Addtime = date ("Y-m-d ");
If ($ imgflag = 1) {// if an image is uploaded
$ Photo = base64_encode ($ randname );
} Else {// otherwise, set the value of the photo element to NONE.
$ Photo = "NONE ";
}
$ Dom = new DOMDocument ('1. 0', 'gb2312'); // specify the XML format
$ Dom-> load ("data. xml"); // load
$ Root = $ dom-> getElementsByTagName ("messages"); // get the root node
$ Root = $ root-> item (0 );
$ Last_id = $ root-> lastChild-> firstChild-> nodeValue; // obtain the value of the first subnode (id node) of the last message.
$ Id = $ last_id + 1; // id of the newly added message
Settype ($ id, "string"); // convert it to struct type
$ Message = $ root-> appendChild (new DOMElement ('message'); // Add a message Node
$ El_id = $ message-> appendChild (new DOMElement ('id'); // Add each subnode of the message Node
$ El_id-> appendChild ($ dom-> createTextNode ($ id ));
$ El_author = $ message-> appendChild (new DOMElement ('author '));
$ El_author-> appendChild ($ dom-> createTextNode ($ author ));
$ El_title = $ message-> appendChild (new DOMElement ('title '));
$ El_title-> appendChild ($ dom-> createTextNode ($ title ));
$ El_smiles = $ message-> appendChild (new DOMElement ('smys '));
$ El_smiles-> appendChild ($ dom-> createTextNode ($ smiles ));
$ El_content = $ message-> appendChild (new DOMElement ('content '));
$ El_content-> appendChild ($ dom-> createTextNode ($ content ));
$ El_addtime = $ message-> appendChild (new DOMElement ('addtime '));
$ El_addtime-> appendChild ($ dom-> createTextNode ($ addtime ));
$ El_photo = $ message-> appendChild (new DOMElement ('photo '));
$ El_photo-> appendChild ($ dom-> createTextNode ($ photo ));
$ Dom-> save ("data. xml"); // save XML
Echo "<meta http-equiv = \" refresh \ "content = \" 2; url = index. php \ "> \ n ";
Echo "thank you for your message. Return to the homepage in 2 seconds ";
}
?>
(3) index. php
This page is used to display the message
<P> <a href = "add. php"> add a message </a> </p>
<? Php
$ Dom = new DOMDocument ('1. 0', 'gb2312 ');
$ Dom-> load ("data. xml"); // load
$ Root = $ dom-> getElementsByTagName ("messages ");
$ Root = $ root-> item (0 );
$ Message = $ root-> getElementsByTagName ("message"); // get all message nodes
$ Message_count = $ message-> length; // calculates the number of messages
Echo "A total of". $ message_count. "messages ";
If ($ message_count = 0 ){
Echo "no message \ n ";
} Else {
?>
<Table border = "1" width = "700">
<? Php
For ($ I = $ message_count-1; $ I >=0; $ I --) // we need to sort messages in reverse order
{
$ Msg = $ message-> item ($ I );
Foreach ($ msg-> childNodes as $ child) // subnodes of the message Node
{
If ($ child-> nodeName = "id ")
{
$ Id = $ child-> nodeValue;
}
If ($ child-> nodeName = "author ")
{
$ Author = $ child-> nodeValue;
}
If ($ child-> nodeName = "title ")
{
$ Title = $ child-> nodeValue;
}
If ($ child-> nodeName = "smiles ")
{
$ Smiles = $ child-> nodeValue;
}
If ($ child-> nodeName = "content ")
{
$ Content = $ child-> nodeValue;
}
If ($ child-> nodeName = "photo ")
{
$ Photo = $ child-> nodeValue;
}
If ($ child-> nodeName = "addtime ")
{
$ Addtime = $ child-> nodeValue;
}
}
Echo "<tr> ";
Echo "<td align = left bgcolor = # CCCCFF> ";
Echo $ id. ". ". base64_decode ($ title ). "-". base64_decode ($ author ). "[". $ addtime. "]";
If (isset ($ _ SESSION ["password"]) & $ _ SESSION ["password"]! = "") // If the password is entered, the deletion link is displayed.
{
Echo "[<a href = 'del. php? Id = ". $ id." '> Delete </a>] ";
}
Echo "</td> </tr> ";
Echo "<tr> <td align = left>". base64_decode ($ content). "</td> </tr> ";
If ($ photo! = "NONE ")
{
Echo "<tr> <td align = left> </td> </tr> ";
}
}
?>
<? Php
}
?>
</Table>
<? Php
If (isset ($ _ SESSION ["password"]) & $ _ SESSION ["password"]! = ""){
?>
<P> <a href = "logout. php"> exit management </a> </p>
<? Php
} Else {
?>
<P> <a href = "login. php"> logon management </a> </p>
<? Php
}
?>
(4) Delete messages
<? Php
If (isset ($ _ SESSION ["password"]) & $ _ SESSION ["password"]! = "")
{
$ Dom = new DOMDocument;
$ Dom-> load ("data. xml ");
$ Root = $ dom-> getElementsByTagName ("messages ");
$ Root = $ root-> item (0 );
Foreach ($ root-> childNodes as $ msg)
{
If ($ msg-> firstChild-> nodeValue = $ _ GET ["id"]) // if the value of the message node's id sub-node is equal to the id to be deleted
{
$ Photo = $ msg-> lastChild-> nodeValue;
If ($ photo! = "NONE") {// if the message contains an image, delete the image.
$ Photo_path = "upfile/". base64_decode ($ photo );
$ Flag = unlink ($ photo_path );
If ($ flag ){
Echo "image deleted <br> ";
}
}
$ Root-> removeChild ($ msg );
Break;
}
}
$ Dom-> save ("data. xml ");
?>
Message deleted successfully. 2 seconds back to homepage
<Meta http-equiv = "refresh" content = "2; url = index. php">
<? Php
} Else {
?>
You have not logged on yet. 2 seconds back to the login page
<Meta http-equiv = "refresh" content = "2; url = login. php">
<? Php
}
?>