PHP实现网页自动更新块_PHP
能不能让php为你的网页添加一些每隔一段时间就自动更新的内容,以更好的吸引访客呢?实现这一功能并不需要太高深的编程知识,请看以下例子:
(一)英语谚语
让我们来看一个简单的应用:让php程序从数据库中随机提取一行记录,显示在网页上,隔15分钟再去读取另一条记录。英汉对照的谚语是个适合显示的内容。
考虑到放置个人主页的廉价虚拟主机多数不支持MySQL之类的数据库,我们使用文本型数据文件来存放数据,这对于流量不很大的网站来说足够了。建立一个文本文件,命名为english.dat,内容如下:
A bad beginning makes a bad ending. 恶其始者必恶其终。
A bad bush is better than the open field. 有胜于无。
A bad compromise is better than a good lawsuit. 吃亏的和解也比胜诉强。
A bad conscience is a snake in one’s heart. 做贼心虚。
A bad custom is like a good cake, better broken than kept. 坏习惯像鲜馅饼,分食要比保存好。
A bad padlock invites a picklock. 开门揖盗。
……
注意,每行是一条完整的记录,中间不要换行;文件中不能有半角的双引号( " )否则在输出数据时程序会解析出错。网上有很多这样的内容,搜索一下即可,也可以去我的网站http://www.musicfly.net/english.dat下载一个数据文件。
下面就是编写读取数据的程序了。建立一个文本文件,命名为english.php,加入源代码如下:
<?php
$filename = ’english.dat’; //数据文件名
$refresh = 900; //更新周期(以秒为单位,可根据需要自行调整)
$data = file($filename); //将数据文件装入一个数组
$num_lines = count($data); //数据文件行数,也就是记录条数 mt_srand(floor(time() / $refresh));
$id = mt_rand(0, $num_lines - 1); //随机选择一条记录号
$content = chop($data[$id]); //根据记录号选择相应的数据,并截去最后的换行符
echo "document.write(\"$content\");"; //用javascript形式输出数据
?>
将english.dat和english.php存盘,上传至服务器的同一个目录下。
最后一道工序就是让你的网页调用这个php程序来显示内容了。因为php运行的结果是以javascript形式输出的,我们只要把这段php当成一段 javascrupt来调用即可,方法是:用记事本或网页编辑软件打开你要修改的html文件,在你想让动态内容显示的位置加入以下代码:
注意,如果你修改的html文件和english.php不在一个目录下,要在src="..."里注明english.php的相对路径。
把这个修改后的html文件上传至服务器,在浏览器中打开。如果你每一步做得都正确,应该就可以看到数据文件中的内容显示出来了!过15分钟刷新一下页面看看,是不是显示的内容变化了?而你并没有为更新这些内容做什么额外的劳动,php程序为你做了。
如果你的朋友想在自己的网页或者博客上实现同样的功能而又没有支持php的主机,可以通过以的方式调用你主机上的程序来显示同样的内容。
当然,这个程序还可能存在编码方式不兼容的问题,因为内容都是以gb2312编码显示出来的,用在UTF编码的网页上,中文会变成乱码。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Blizzard Battle.net update keeps stuck at 45%, how to solve it? Recently, many people have been stuck at the 45% progress bar when updating software. They will still get stuck after restarting multiple times. So how to solve this situation? We can reinstall the client, switch regions, and delete files. To deal with it, this software tutorial will share the operation steps, hoping to help more people. Blizzard Battle.net update keeps stuck at 45%, how to solve it? 1. Client 1. First, you need to confirm that your client is the official version downloaded from the official website. 2. If not, users can enter the Asian server website to download. 3. After entering, click Download in the upper right corner. Note: Be sure not to select Simplified Chinese when installing.

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

A friend's computer has such a fault. When opening "This PC" and the C drive file, it will prompt "Explorer.EXE Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the project." Including folders, files, This computer, Recycle Bin, etc., double-clicking will pop up such a window, and right-clicking to open it is normal. This is caused by a system update. If you also encounter this situation, the editor below will teach you how to solve it. 1. Open the registry editor Win+R and enter regedit, or right-click the start menu to run and enter regedit; 2. Locate the registry "Computer\HKEY_CLASSES_ROOT\PackagedCom\ClassInd"

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

Executing PHP code in a web page requires ensuring that the web server supports PHP and is properly configured. PHP can be opened in three ways: * **Server environment:** Place the PHP file in the server root directory and access it through the browser. * **Integrated Development Environment: **Place PHP files in the specified web root directory and access them through the browser. * **Remote Server:** Access PHP files hosted on a remote server via the URL address provided by the server.
