建设一个简单交互的网站(六)
9. 简易banner动态更替
不知大家有没有发现各大站点上的标头广告banner,我们每次访问这些站点时,都会看到不同的广告图标,或者如果你每次刷新页面时,这些广告banner就会不断地随机更替变换。要实现这种效果虽然用javascript也可以达到(象天极网站的动态变换广告banner就是通过调用javascript来实现的),但是如果我们用PHP的话,我们还可以结合数据库来做数据量很大,如每日一题之类的功能。费话少说,让我们立即来看看如何用PHP来实现banner的动态更替功能。
简易banner动态更替PHP文件(banner.php3):
//产生随机数
srand((double)microtime()*1000000);
//在0和4之间取一个数字
$randval = rand(0,5);
// 显示结果
echo "";
?>
我们可以发现,实现的程序非常简单:主要是先利用srand这一初始化随机数产生器产生随机数,再调用rand函数在定义的有效范围内来获取其中一个随机值,最后显示$randval.gif各图片banner,即0.gif、1.gif、2.gif、3.gif或4.gif。为了便于大家理解,我将rand函数的语法及相关说明罗列如下:
rand
语法:int rand([int min], [int max]);
返回值:整数
函数种类:数学运算
内容说明:本函数用来取得随机值。若没有指定随机数的最大及最小范围,本函数会自动地从0到RAND_MAX中取一个随机数。若有指定min及max的参数,则从指定参数中取一数字。例如rand(38,49)则会从38到49之间取得一个随机值。其中UNIX系统包含49,Win32系统不包含49。值得注意的是为了使随机数的随机率最大,每次在取随机数前最好使用srand()来设定新的随机数。在本例中在用srand()来产生新的随机数时加入了时间因素,执行时以百万分之一的随机率来产生随机数
9.1 我们更改head.inc文件以应用该简易banner动态更替功能,同时还得为不同的广告banner链接到它们对应的网址。
当然,首先我们必须先准备好用于更换交换的banner图标,同时也给我们的页面标头加上自己网站的徽标(01DC.gif)。
新的标头文件(header.inc):
// 定义通用页面头部
?>
html>
The above introduces the tips for newbies to get started with PHP (10), including the tips for newbies to get started. I hope it will be helpful to friends who are interested in PHP tutorials.

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


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

WebStorm Mac version
Useful JavaScript development tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
