Message Book Small Experiment

Source: Internet
Author: User
Tags php open file

Experimental Purpose:

PHP implementation of the message, and there is a txt text file, you can also read the message from a text file, and displayed on the page.

Experiment Code:

The first to use a form to submit a message, write a simple HTML as follows:

1 <!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd ">2 <HTMLxmlns= "http://www.w3.org/1999/xhtml"Xml:lang= "en">3 <Head>4     <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8">5     <title>Form submission</title>6 </Head>7 <Body>8 <formAction= "postmsg.php"Method= "POST">9     <P>Message title:<inputtype= "text"name= "title" /></P>Ten     <P>Message content:<textareaname= "Content"ID=""cols= "+"rows= "Ten"></textarea></P> One     <P><inputtype= "Submit"value= "Submit" /></P> A </form> -      - </Body> the  - </HTML>
postmsg.html

Then submit the data to where, action= "postmsg.php" indicates to submit to postmsg.php, and then process the submitted data, processing the following code comment section:

1<?PHP2 Header("content-type:text/html; Charset=utf-8 ");//Set page content is HTML encoded format is Utf-83 4 //Print_r ($_post);5 6 //Start message7 //define storage format, stored as string "Title,content" on a text document8 $str=$_post[' title ']. "," .$_post[' content ']. "\ n";9 Ten //PHP Open File One //FH is a variable that is a variable of the resource type A $fh=fopen('./msg.txt ', ' a '); -  - //write something in a file, write it along a pipe (resource) the fwrite($fh,$str); -  - //Close Resource - fclose($fh); +  - //prompt message is successful, and there is a return button + Echo"Message Success", "\ n", ' <input type= "button" value= "return" onclick= "JavaScript:history.go ( -1)" > "; A  at?>
postmsg.php

This allows you to see messages under the Msg.txt text file under the current file.

Next is how to read the message, in the msg.php to traverse the entire message book, and show Out, the best is added to the link, click into the message directly into the comments:

1<?PHP2 Header("content-type:text/html; Charset=utf-8 ");//Set page content is HTML encoded format is Utf-83 4 //define variable TID to indicate the number of the message5 $tid=$_get[' Tid '];6 7 //Echo ' You want to see the first ', $tid, ' line message ';8 9 //Open Get fileTen $fh=fopen('./msg.txt ', ' R '); One  A $i= 1; - //iterate as a list and display the message title - Echo' <ol> '; the  while(($row=Fgetcsv($fh)) !=false) { -  -     Echo' <li><a href= ' readmsg.php?tid= ',$i, ' > ',$row[0], ' </li> '; -     $i++;  + } - Echo' </ol> '; +  A?>
msg.php

Finally, read the message:

1<?PHP2 Header("content-type:text/html; Charset=utf-8 ");3 4 $tid=$_get[' Tid '];5 6 //Echo ' You want to see the first ', $tid, ' line message ';7 8 //Open Get file9 $fh=fopen('./msg.txt ', ' R ');Ten  One $i= 1; A  -  while(($row=Fgetcsv($fh)) !=false) { -  the     if($i==$tid){ -         Echo' $row[0], ' ; -         Echo' <p> ',$row[1], ' </p> '; -     } +     $i++; - } +  A?>
readmsg.phpExperiment Summary:
  1. About $_post and $_get differences:

    $_get
    Is the address of the value, with '? ' to start the value, multiple values with ' & ' separated, more for simple transmission value, for example, see news need News ID general will use address value, $_get the benefit is the value is visible, that is, as long as an address is OK, easy to share, the disadvantage is not safe, And there is a limit to the number of bytes.

    $_post
    This type of submission is not visible, that is, the value of the pass will not appear in the URL address, generally used for registration and login, this way because it is more secure to a point, the general will use post, and the Post method does not limit the number of bytes, fill in the article, you need to submit big data, the form contains the upload file, will use post.

  2. fopen (), fwrite (), and fclose () functions:

    The fopen () function opens the file or URL. If the open fails, this function returns FALSE. Syntax: fopen (filename,mode,include_path,context), see the PHP fopen () function specifically

    The fwrite () function writes to the file (which is safe for binary files). The syntax is: fwrite (file,string,length), see the PHP fwrite () function specifically

    The fclose () function closes an open file. The syntax is: fclose (file), see PHP fclose () function

  3. Fgetcsv () function

    The fgetcsv () function reads a row from the file pointer and resolves the CSV field. Similar to Fgets (), the difference is that fgetcsv () parses a read-in row and finds a field in CSV format, and then returns an array containing those fields. Fgetcsv () returns FALSE when an error occurs, including when the file ends. The syntax is: fgetcsv (file,length,separator,enclosure), see PHP fgetcsv () function specifically

  4. This experiment still has a lot of bugs, the function is not very perfect, just a small experiment to get started with PHP, I would like to use this as my learning trajectory.

Message Book Small Experiment

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.