Php+xml make a simple message this text tutorial _php example

Source: Internet
Author: User
Tags hash

1. Message Display page

2. Post a message and allow uploading of pictures

3. Enter Password login can delete message.

1. File directory

Upfile is the directory where the uploaded images are saved.

2. Main interface

(1) Home, show the Message page

(2) Post the 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 the base64_encode to encode the string.

4 Main Page code

(1) add.php

This page is just pure HTML code

<formaction= "saveadd.php" enctype= "Multipart/form-data" method= "POST"Name= "MyForm" onsubmit= "Return to go" >
<table border= "1" width= ">"
<tr>
<td> author </td>
&LT;TD align= "left" ><input type= "text" name= "author" size= "Ten" ></td>
</tr>
<tr>
<td> title </td>
&LT;TD align= "left" ><input type= "text" name= "title" Size= "M" ></td>
</tr>
<tr>
<td> Facial Expression </td>
&LT;TD align= "Left" >
<select name= "smiles" size= "1" onchange= "change_img" (); " >
<option value= "Smile.gif" > Smile </option>
<option value= "Biggrin.gif" > Upright </option>
<option value= "Victory.gif" > Victory </option>
<option value= "tongue.gif" > Tongue </option>
<option value= "Titter.gif" > Giggle </option>
<option value= "Cry.gif" > Weeping </option>
<option value= "Curse.gif" > Angry </option>
<option value= "Huffy.gif" > Rage </option>
<option value= "Mad.gif" > Madness </option>
<option value= "Sad.gif" > Sadness </option>
<option value= "Shocked.gif" > Shock </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>
&LT;TD align= "left" ><textarea name= "content" cols= "a" rows= "ten" ></textarea></td>
</tr>
<tr>
<td> Screenshots </td>
&LT;TD align= "left" ><input type= "file" Name= "Upfile" size= "M" ></td>
</tr>
<tr>
&LT;TD colspan= "2" ><input type= "Submit" value= "submitted"/></td>
</tr>
</table>
</form>

(2) savadd.php

For saving message information

<?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 to return home";
Exit ();
}else{
$imgflag = 0; To determine if you need to upload a picture
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 + +)//Find length characters randomly from the string above
{
$hash. = $chars [Mt_rand (0, $max)];
}
return $hash;
}

function Fileext ($filename)//This is used 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");  //file type allowed to upload

  if (Strtolower (Fileext ($_files[' upfile '] [' name ']), $type))    If the uploaded file has an extension that 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];
     $randname =implode (".", $filename);    //final randomly generated file name (along with extension)
     $uploadfile = $uploaddir. $randname;
   } while (File_exists ($uploadfile));

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

}
}

Get 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 ("Untitled");
}
$addtime =date ("y-m-d");
if ($imgflag ==1) {//If there are uploaded images
$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"); Get 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; ID of the new message
Settype ($id, "string"); Convert it to 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 "<meta http-equiv=\" refresh\ "content=\" 2;url=index.php\ ">\n";
echo "Thank you for your message, 2 seconds to return home";

}
?>

(3) index.php

This page is used to display message information

<p><a href= "add.php" > Add 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; How many messages are counted
echo "Currently in common". $message _count. " Message ";

if ($message _count==0) {
echo "No message at the moment \ \";
}else{
?>
<table border= "1" width= ">"
<?php
for ($i = $message _count-1 $i >=0; $i-)//We need to send the message in reverse order
{
$msg = $message->item ($i);

foreach ($msg->childnodes as $child)//message nodes of a 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 a password is entered to display the deletion link
{
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" > Login Management </a></p>
<?php
}
?>

(4) Delete message

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

    $dom =new domdocument; 
    $dom->load ("Data.xml");   &NBSP;&NBSP;&NBSP;&NBSP
    $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 success <br>";
&NBSP;&NBSP;&NBSP;&NBSP}
   }

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


?>

Delete message successful, 2 seconds to return to the home page
<meta http-equiv= "Refresh" content= "2;url=index.php" >
<?php
}else{
?>
You have not logged in, 2 seconds to return to the landing page
<meta http-equiv= "Refresh" content= "2;url=login.php" >
<?php
}
?>

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.