PHP + XML simple message text tutorial _ php instance

Source: Internet
Author: User
This example demonstrates how PHP can manipulate XML. The message book has the following basic functions: 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

(2) savadd. php

Used to save the message

If (! $ _ POST ["author"] |! $ _ POST ["content"])
{
Echo" \ 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" \ N ";
$ Text = implode (",", $ type );
Echo "you can only upload the following types of files:", $ text ,"
";
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 ","
", $ _ 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" \ 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

Add message

$ 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 {
?>

















































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" ";Echo" ";Echo" ";If ($ photo! = "NONE "){Echo" ";}}?> }?>
";
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 "[delete]";
}
Echo"
". Base64_decode ($ content )."

If (isset ($ _ SESSION ["password"]) & $ _ SESSION ["password"]! = ""){
?>

Exit Management


} Else {
?>

Login management


}
?>

(4) delete messages

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 successfully
";
}
}

$ Root-> removeChild ($ msg );
Break;
}
}
$ Dom-> save ("data. xml ");


?>

Message deleted successfully. 2 seconds back to homepage

} Else {
?>
You have not logged on yet. 2 seconds back to the login page

}
?>

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.