PHP implements active block update for webpages

Source: Internet
Author: User
Nowadays, many friends who have personal homepages or blogs choose to rent virtual hosts, while a considerable number of virtual hosts support php. If you have a host space supporting php, are you using php to arm your website? Can php help you preface: Now many friends who have personal homepages or blogs choose to rent virtual hosts, while a considerable number of virtual hosts support php. If you have a host space supporting php, are you using php to arm your website?

Can I allow php to add some content that is automatically updated on your webpage at regular intervals to attract visitors? To achieve this function, you do not need to have too much advanced programming knowledge. please refer to the following example:

(1) English slang

Let's take a look at a simple usage: Let the php program randomly extract a row of records from the database, display them on the webpage, and read another record every 15 minutes. English-Chinese slang is suitable for display.

Considering that most of the cheap virtual hosts placed on the personal homepage do not support databases such as MySQL, we use text data files to store data, which is sufficient for websites with low traffic. Create a text file named english. dat with the following content:

A bad beginning makes a bad ending. the beginning of evil will be evil.

A bad bush is better than the open field.

A bad compromise is better than a good lawsuit. The loss settlement is better than the victory.

A bad conscience is a snake in one's heart.

A bad custom is like a good cake, better broken than kept. bad habits like fresh pies are better than retaining them.

A bad padlock invites a picklock. bring the wolf into the room.

......

Note that each line is a complete record, so do not wrap it in the middle. the file cannot contain double quotation marks ('); otherwise, the program will encounter parsing errors when outputting data. There is a lot of such content on the internet. you can search for it and download a data file to my website http://www.musicfly.net/english.dat.

The following is a program for reading data. Create a text file named english. dat. the source code is as follows:

<? Php
$ Filename = 'English. dat '; // data file name
$ Refresh = 900; // update cycle (in seconds, you can adjust it as needed)
$ Data = file ($ filename); // load the data file into an array
$ Num_lines = count ($ data); // number of data file lines, that is, the number of records mt_srand (floor (time ()/$ refresh ));
$ Id = mt_rand (0, $ num_lines-1); // select a random record number.
$ Content = chop ($ data [$ id]); // select the corresponding data based on the record number and cut it to the last line break

Echo 'document. write (\ '$ content \'); '; // use javascript to output data.
?>


Upload the english. dat and english. php files to the same directory on the server.

The last step is to let your webpage call this php program to display the content. Because the results of php running are output in javascript, we only need to use this php as a javascrupt to call it. the method is to use notepad or web page compilation software to open the html file you want to modify, in your position to display dynamic content, add the following code:

<Script language = 'javascript 'src = 'English. php'> </script>

Note that if the modified html file and english. php are not in the same directory, you must specify the relative path of english. php in src =.

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.