选择显示字体大小

php新手上路(十)


9. 简易banner动态更替

  不知大家有没有发现各大站点上的标头广告banner,我们每次访问这些站点时,都会看到不同的广告图标,或者如果你每次刷新页面时,这些广告banner就会不断地随机更替变换。要实现这种效果虽然用javascript也可以达到(象天极网站的动态变换广告banner就是通过调用javascript来实现的),但是如果我们用php的话,我们还可以结合数据库来做数据量很大,如每日一题之类的功能。费话少说,让我们立即来看看如何用php来实现banner的动态更替功能。

简易banner动态更替php文件(banner.php3):
<?
//产生随机数
srand((double)microtime()*1000000);
//在0和4之间取一个数字
&#36;randval = rand(0,5);
// 显示结果
echo &quot;<a href=//gophp.heha.net/index.html><img alt=进入php的世界 border=0 src=&#36;randval.gif></a>&quot;;
?>

  我们可以发现,实现的程序非常简单:主要是先利用srand这一初始化随机数产生器产生随机数,再调用rand函数在定义的有效范围内来获取其中一个随机值,最后显示&#36;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>
<head>
<title> <? echo &quot;&#36;mysitename - &#36;title&quot;; ?> </title>
<style type=&quot;text/css&quot;>
<!--
.text { font-family: &quot;宋体&quot;; font-size: 12pt; color: #006633; text-decoration: none}
-->
</style>
</head>
<body topmargin=2>
<table width=&quot;100%&quot; border=&quot;0&quot;>
<tr>
<td rowspan=&quot;3&quot; width=&quot;19%&quot;><img src=&quot;/showimg.asp?p=/2006-3-29/65644gesa01dc.gif&quot; width=&quot;140&quot; height=&quot;60&quot;></td>
<td rowspan=&quot;3&quot; width=&quot;29%&quot;>
<?
//取得乱数种子
srand((double)microtime()*1000000);
//在0和4之间取一个数字
&#36;randval = rand(0,5);
// 显示结果
switch(&#36;randval)
{
case &quot;0&quot;;
echo &quot;<a href=//gophp.heha.net/index.html><img border=0 src=&#36;randval.gif></a>&quot;;
break;
case &quot;1&quot;;
echo &quot;<a href=//personal.668.cc/haitang/index.htm><img border=0 src=&#36;randval.gif></a>&quot;;
break;
case &quot;2&quot;;
echo &quot;<a href=//gophp.heha.net/index.html><img border=0 src=&#36;randval.gif></a>&quot;;
break;
case &quot;3&quot;;
echo &quot;<a href=//gophp.heha.net/index.html><img border=0 src=&#36;randval.gif></a>&quot;;
break;
case &quot;4&quot;;
echo &quot;<a href=//personal.668.cc/haitang/index.htm><img border=0 src=&#36;randval.gif></a>&quot;;
break;
}
?>
</td>
<td width=&quot;52%&quot;>
<div align=&quot;center&quot;><a href=&quot;../test/form.php3&quot; class=&quot;text&quot;>自动发送邮件测试</a></div>
</td>
</tr>
<tr>
<td width=&quot;52%&quot;>
<div align=&quot;center&quot;><a href=&quot;../test/php/php1.php3&quot; class=&quot;text&quot;>简易轮回广告更替</a></div>
</td>
</tr>
<tr>
<td width=&quot;52%&quot;>
<div align=&quot;center&quot;><a href=&quot;../password/password.php3&quot; class=&quot;text&quot;>简易密码验证实例</a></div>
</td>
</tr>
</table>
<hr color=&quot;#ff9900&quot; size=&quot;4&quot;>
</body>
</html>

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons