Php+xml make a simple message text tutorial _php Tutorial

Source: Internet
Author: User
1. Message Display page

2. Post a message and allow uploading of pictures

3. Enter the password to log in and delete the message.

1. File directory

Upfile is the directory where the uploaded images are saved.

2. Main interface

(1) Homepage, display message page

(2) Post Message page

3. XML document format, named Data.xml

The meaning of each field is not much, the value of each element looks a bit strange, because I used the 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 message information

if (!$_post["author"] | |!$_post["CONTENT"])
{
echo " \ n";
echo "You did not fill in the message name or content, 2 seconds to return home";
Exit ();
}else{
$imgflag = 0; Used to determine if a picture needs to be uploaded
Functions Random ($length)//This function is used to generate a random picture file name (without extension) to prevent duplication with existing pictures
{
$hash = ' img-';
$chars = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';
$max = strlen ($chars)-1;
for ($i = 0; $i < $length; $i + +)//randomly find length characters from the string above
{
$hash. = $chars [Mt_rand (0, $max)];
}
return $hash;
}

function Fileext ($filename)//To get the extension of the uploaded file
{
Return substr (STRRCHR ($filename, '. '), 1);
}

if ($_files["Upfile" ["Name"]!= "") {
$uploaddir = "upfile/"; Picture Save Path
$type =array ("JPG", "gif", "BMP", "JPEG", "PNG"); Allowed file types to upload

if (!in_array (Strtolower (Fileext ($_files[' upfile ' [' name '])), $type))//If the uploaded file's extension 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); The resulting randomly generated file name (along with the extension)
$uploadfile = $uploaddir. $randname;
} while (File_exists ($uploadfile));

if (Move_uploaded_file ($_files[' upfile ' [' tmp_name '], $uploadfile)) {//Save the uploaded picture to the Upfile folder
echo "Upload image success";
$imgflag = 1;
}
else{
echo "failed to upload image! ";
$imgflag = 0;
}

}
}

Get additional 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 ("Untitled");
}
$addtime =date ("y-m-d");
if ($imgflag ==1) {//If there is a picture 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 format of the XML
$dom->load ("Data.xml");//load
$root = $dom getElementsByTagName ("Messages"); Gets the root node
$root = $root->item (0);
$last _id= $root->lastchild->firstchild->nodevalue;//Gets the value of the first child node (that is, the ID node) of the last message
$id = $last _id+ 1; The ID of the new message
Settype ($id, "string");//convert it to a character type


$message = $root->appendchild (new domelement (' message ')); Add Message Node
$el _id= $message->appendchild (new domelement (' id ')); Add each child node 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 (' smiles '));
$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, 2 seconds to return home";

}
?>

(3) index.php

This page is used to display message information

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; Calculate how many messages you have
echo "Currently in common". $message _count. " Message ";

if ($message _count==0) {
echo "No message at the moment \ 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) the individual 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 you enter a password to display the delete link
{
echo "[Delete]";
}
echo "
". Base64_decode ($content)."

if (isset ($_session["password") && $_session["password"]!= "") {
?>

Exit management


}else{
?>

Login Management


}
?>

(4) Delete message

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 ID child node of the message node is equal to the ID to be deleted
{
$photo = $msg->lastchild->nodevalue;
if ($photo! = "NONE") {//If the message contains a picture, you should also delete the picture
$photo _path= "upfile/". Base64_decode ($photo);
$flag =unlink ($photo _path);
if ($flag) {
echo "Delete picture succeeded
";
}
}

$root->removechild ($msg);
Break
}
}
$dom->save ("Data.xml");


?>

Delete message successful, 2 seconds back to home

}else{
?>
You have not logged in, 2 seconds to return to the landing page

}
?>

http://www.bkjia.com/PHPjc/320900.html www.bkjia.com true http://www.bkjia.com/PHPjc/320900.html techarticle 1. Message Display Page 2. Post a message and allow to upload a picture 3. Enter the password to log in and delete the message. 1. File directory upfile is the directory where the uploaded images are saved. 2. Main interface (...

  • Related Article

    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.