banner 广告轮播
现在很多网站提供banner广告轮播的类似 ASP 服务(application service provider)
可时常会发生问题,原因是提供用户越多,服务器负担越重,反馈时间就越长,很容易造成叶面中断
浏览器提示无法显示,在刷新就只显示banner了很烦人,我们自己有服务器,幼执行权限,干嘛用人家的
本人研究了一下,发现也没什么难的,放到自己的服务器上肯定很快
由于当初在网上查找时没有发现好的例子大部分都没有太多控制,所以就自己写了一个,虽不如好耶的功能多
外表华丽,可大部分也能实现,而且提简单方便的,现在提供给大家:
本程序运行 linux+php+mysql 下:
1)db:///////////////////////////////////////////////////////////////////////////////////////////////////////////
Database present2 - table banners
Field Type Attributes Null Default Extra Action 
bannerID  mediumint(9)    No  0    Change Drop Primary Index Unique 
clientID  mediumint(9)    No  0    Change Drop Primary Index Unique 
width  smallint(6)    No  0    Change Drop Primary Index Unique 
height  smallint(6)    No  0    Change Drop Primary Index Unique 
linkurl  varchar(255)    No      Change Drop Primary Index Unique 
imagesurl  varchar(200)    No      Change Drop Primary Index Unique 
rate  int(11)    No  0    Change Drop Primary Index Unique 
sepage  varchar(250)    Yes      Change Drop Primary Index Unique 
startdate  date    No  0000-00-00    Change Drop Primary Index Unique 
closedate  date    No  0000-00-00    Change Drop Primary Index Unique 
border  int(11)    No  0    Change Drop Primary Index Unique 
id  int(11)    No  0  auto_increment  Change Drop Primary Index Unique 
browseIP  varchar(80)    No      Change Drop Primary Index Unique 
starttime  time    No  00:00:00    Change Drop Primary Index Unique 
closetime  time    No  00:00:00    Change Drop Primary Index Unique 
timeflag  int(1)    No  0    Change Drop Primary Index Unique 

Keyname Unique Field Action 
PRIMARY Yes id Drop 

2)banner.php ///////////////////////////////////////////////////////////////////////////////////////////
<? include("database2.php");
  
$URL=trim($URL); 
  
 
$url0=parse_url($URL);
// echo $url0["path"]."<br>";
 
 
 
 
   

   
$pageurl=$url0["path"];
//echo $pageurl;
  
$nummax=86400;
  
$banners=array();
  
$asum=0;  //rate value sum
  
$today=date("Y-m-d H:i:s"); 
  
$totime=date("H:i:s");
  
  
$strtime=" and IF(timeflag=1,starttime<='$totime' && closetime>='$totime',1=1)";
 
// $sepage=" and IF(timeflag=1,starttime<='$totime' && closetime>='$totime',1=1)";
  
$startrange=0;
  
$endrange=0;   
  
$flagsppage=1;
  
$i=0;
  
// echo $today."<br>";
  
$sql="select * from banners where startdate<='$today'  and  closedate>='$today' and rate<>0 and sepage like '$pageurl' and bannerID=$bannerID $strtime";
  
    
$result=mysql_query($sql,$db);

   if (
mysql_num_rows($result)<1)
   { 
$flagsppage=0;
    
 
$sql="select * from banners where startdate<='$today'  and  closedate>='$today' and rate<>0 and (sepage  IS NULL or  sepage='') and bannerID=$bannerID $strtime";
     
 
$result=mysql_query($sql,$db); 
 
       }    
  
// echo "sql=".$sql;
 
 
while ($row=mysql_fetch_array($result)) 
 { 
 
 
$banners[$i][0]=$row["bannerID"]; 
 
$banners[$i][1]=$row["width"];
 
$banners[$i][2]=$row["height"];
 
$banners[$i][3]=$row["linkurl"];
 
$banners[$i][4]=$row["imagesurl"];
 
$banners[$i][5]=$row["rate"];
 
//$banners[$i][6]=$row["sepage"];
 
$banners[$i][7]=$row["border"];
  
// echo   $banners[$i][4] ;
  // echo  "<br>"  ;
 
$i++;

   
}  
$bannernum=$i 
//echo $bannernum ;
mt_srand((double)microtime()*1000000);
$randnum=mt_rand(1,$nummax);

for(
$a=0;$a<$bannernum;$a++)
{
 
$anum=$anum+$banners[$a][5];
}
 for(
$c=0;$c<$bannernum;$c++)
{  
$startrange=$endrange;
   
$endrange=$startrange+ceil($nummax*$banners[$c][5]/$anum); 
   if (
$randnum>$startrange-&& $randnum<$endrange+1)
     { 
$point=$c;
      break;
  }
         
//echo $c."  ".$startrange."   ".$endrange."<br>";
 
}
 
//echo $randnum."<br>";
 

if ($ponit>=&& $point<$bannernum)
{  
   if  (
$banners[$point][3]=="")
   {  
    
?>
     <img src='<? echo trim($banners[$point][4]) ?>' height='<? echo trim($banners[$point][2]) ?>' width  =   '<? echo trim($banners[$point][1]) ?>' border='<? echo trim($banners[$point][7]) ?>'>
    <?
    
}
    else
    {

    
?>
    <a href='<? echo trim($banners[$point][3]) ?>' target=_blank><img src='<? echo trim($banners[$point][4]) ?>' height='<? echo trim($banners[$point][2]) ?>' width  =   '<? echo trim($banners[$point][1]) ?>' border='<? echo trim($banners[$point][7]) ?>'></a>
 
 
 
    <?
    
}

}
 else
 {
 echo 
" program err";
 }

?>
//////////////////////////////////////////////////////////////////////////////////////////////////////
3)database2.php
<?  
$db
=mysql_connect("localhost","***","***") or die("无法连接数据库服务器!");
mysql_select_db("******",$db) or die("无法连接数据库服务器!");
?>
///*********** 提换成你的配置



///////////////////////////////////////////////////////////////////////////////////////////////////
4)adsbrow.php
 
<style type="text/css">
<!--
.unnamed1 {  font-family: "宋体"; font-size: 9pt}
-->
</style>

<form name=banner action="./adsbrow.php">
  <p align="center"><b><font color="#333399">广告轮番播出管理</font></b></p>
  <hr size="1">
  <table border=1 class="unnamed1" cellpadding="0" cellspacing="0" width="780" >
    <tr bgcolor="#00CCFF" >
      <td width="18">序号</td>
      <td width="6">BannerID</td>
      <td width="210">图片或动画 </td>
      <td width="210">连接</td>
      <td width="200">播出页面</td>
      <td width="18">删除</td>
    </tr>
    <?
  
include("database2.php");
 
$sql="select * from banners";
 
$res=mysql_query($sql,$db);
 while(
$row=mysql_fetch_array($res))
 { echo 
"<tr><td>&nbsp;<a href='./adsdetail.php?id=".$row["id"]."'>**".$row["id"]."**</a></td><td>&nbsp;".$row["bannerID"]."</td><td>&nbsp;".$row["imagesurl"]."</td><td>&nbsp;".$row["linkurl"]."</td><td>&nbsp;".$row["sepage"]."</td><td>&nbsp;<a href='./adsdetail.php?del=1&id=".$row["id"]."'>**".$row["id"]."**</a></td></tr>";
 
 
 }

?> 
  </table>
  <div align="center"><a href="ads.php"><br>
    添加新BANNER </a> </div>
</form>
////////////////////////////////////////////////////////////////////////////////////////////////////////
5)adsdetail.php
<script language="javascript"> 
function chkd(oo,pstr)
      {
           if (oo!="")
          { ee=0;
                    flag=0;
      
                 while ( ee <oo.length)
                       {
     
                         if(oo.charCodeAt(ee)<48 || oo.charCodeAt(ee)>57)
                              {
                                  flag=1; 
         
                                  }
                         if (flag)
                              { alert(pstr);
                                      return true;
                                   }
                             ee++;
                                 }
                 }    
        
        }  
        
function ckk()
{        
 if (document.banner.bannerID.value.length<1)
   {
        alert("bannerID not empty"); 
        return false;
    } 
   if (chkd(document.banner.elements("bannerID").value,"bannerID请用数字! :-)" ))
          return false;
 //  if (document.banner.linkurl.value.length<1)
 //       {
 //       alert("连接 not empty"); 
 //       return false;
 //        }          
        
    if (document.banner.width.value.length<1)
       {
         alert("width not empty"); 
         return false;
        }        
    if (chkd(document.banner.elements("width").value,"宽度请用数字! :-)" ))
         return false;
               
    if (document.banner.height.value.length<1)
      {
          alert("height not empty"); 
          return false;
      }        
    if (chkd(document.banner.elements("height").value,"高度请用数字! :-)" ))
         return false;
             
               
    if (document.banner.imagesurl.value.length<1)
        {
        alert("图片路径 not empty"); 
        return false;
         }  
     if (document.banner.rate.value.length<1)
      {
       alert("播放比率 not empty"); 
       return false;
       }  
     if (chkd(document.banner.elements("rate").value,"播放比率请用数字! :-)" ))
         return false;
         
     if (document.banner.border.value.length<1)
        {
        alert("border not empty"); 
        return false;
         }  
    if (chkd(document.banner.elements("border").value,"border请用数字! :-)" ))
        return false;
        else
        return true ;  
        }        
        </script>
<?
  
include("database2.php"); 
 function 
ck($ud)
 {
    
$ud=trim($ud);
    if (
strlen($ud)<2)
    {
     
$ud="0".$ud;
    }
   return 
$ud;
 }
  
$startdate=ck($startyear)."-".ck($startmonth)."-".ck($startday);
  
$closedate=ck($endyear)."-".ck($endmonth)."-".ck($endday);
 
$starttime=ck($starth).":".ck($startm).":".ck($starts);
  
$endtime=ck($endh).":".ck($endm).":".ck($ends);
 if (
$del==1)
 {
 
 
$sql="delete from banners where id='$id'";
 if (
mysql_query($sql,$db))
 { echo 
"success delete '$id' ";
 }
 }
 
 
 
 if (
$submit=="修改")
 {  
 if (
$timeflag=="")
     {  
$timeflag=0; }
 
 
$sql="update banners set bannerID=".$bannerID.",width=".$width.",height=".$height.",linkurl='$linkurl',imagesurl='$imagesurl',rate=".$rate.",sepage='".$sepage."',startdate='$startdate',closedate='$closedate',border=".$border.",starttime='$starttime',closetime='$closetime',timeflag=".$timeflag." where id=".$id;
//echo $sql;  
if(!$rs=mysql_query($sql,$db))
{
 echo 
"执行失败 please contact with webmaster";
}
else
echo 
"add the record success   !!! ;-)";  
}  
$sql="select * from banners where id=".$id

$rs=mysql_query($sql,$db);
$row=mysql_fetch_array($rs);
$startdate=explode("-",$row["startdate"]);  

$starty$startdate[0]; 
$startmd$startdate[1];
$startdsubstr($startdate[2],0,2);  

 
$closedate=explode("-",$row["closedate"]);

$endy$closedate[0]; 
$endmh$closedate[1];
$enddsubstr($closedate[2],0,2); 

 
$closetime=explode(":",$row["closetime"]); 

$endh$closetime[0]; 
$endm$closetime[1];
$endssubstr($closetime[2],0,2);
 
$starttime=explode(":",$row["starttime"]); 
 
$starth$starttime[0]; 
$startm$starttime[1];
$startssubstr($starttime[2],0,2);

?> 
<style type="text/css">
<!--
.unnamed1 {  font-family: "宋体"; font-size: 9pt}
-->
</style>

 <center>
  <b> <font size="4">Banner 广告浏览修改</font></b> 
  <hr>

<form name=banner action=adsdetail.php> 

    <table class="unnamed1" cellpadding="0" cellspacing="0" border="1">
      <tr> 
        <td bgcolor="#33CCFF"> bannerID </td>
        <td bgcolor="#009999"> 
          <input type=text name=bannerID size="15" value="<? echo $row["bannerID"?>" >
        </td>
      </tr>
      <tr> 
        <td bgcolor="#33CCFF"> 图片或动画 </td>
        <td bgcolor="#009999"> 
          <input type=text name=imagesurl size="50"  value="<? echo $row["imagesurl"?>">
        </td>
      </tr>
      <tr> 
        <td bgcolor="#33CCFF"> 图片尺寸 </td>
        <td bgcolor="#009999"> 宽 
          <input type=text name=width size="12"  value="<? echo $row["width"?>">
          高 
          <input type=text name=height size="12"  value="<? echo $row["height"?>">
        </td>
      </tr>
      <tr> 
        <td bgcolor="#33CCFF">图片边框</td>
        <td bgcolor="#009999"> 
          <input type=text name=border value="<? echo $row["border"?>" >
        </td>
      </tr>
      <tr> 
        <td rowspan="2" bgcolor="#33CCFF"> 链结地址 </td>
        <td rowspan="2" bgcolor="#009999"> 
          <input type=text name=linkurl size="50" value="<? echo $row["linkurl"?>">
        </td>
      </tr>
      <tr> </tr>
      <tr> 
        <td bgcolor="#33CCFF"> 播出比率(权值) </td>
        <td bgcolor="#009999"> 
          <input type=text name=rate value="<? echo $row["rate"?>">
        </td>
      </tr>
      <tr> 
        <td bgcolor="#33CCFF"> 播出页面 </td>
        <td bgcolor="#009999"> 
          <input type=text name=sepage size="50" value="<? echo $row["sepage"?>">
        </td>
      </tr>
      <tr> 
        <td bgcolor="#33CCFF"> 开始播出时间 </td>
        <td bgcolor="#009999"> <?
                
echo "<select  name=startyear>";
                   for (
$i=2000;$i<=2010;$i++)
                   {
                         if (
$i==intval($starty))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>年";
                  echo 
" <select  name='startmonth'>";
                   for (
$i=1;$i<=12;$i++)
                   {
                         if (
$i==intval($startmd))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option   value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>月";
                    echo 
" <select  name='startday'>";
                   for (
$i=1;$i<=31;$i++)
                   {
                 if  (
$i==intval($startd))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option   value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>日";
                  
?> </td>
      </tr>
      <tr> 
        <td bgcolor="#33CCFF"> 停止播出时间 </td>
        <td bgcolor="#009999"> <?
                
echo "<select  name=endyear>";
                   for (
$i=2000;$i<=2010;$i++)
                   {
                         if (
$i==intval($endy))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>年";
                  echo 
" <select  name='endmonth'>";
                   for (
$i=1;$i<=12;$i++)
                   {
                         if (
$i==intval($endmh))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option   value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>月";
                    echo 
" <select  name='endday'>";
                   for (
$i=1;$i<=31;$i++)
                   {
                 if  (
$i==intval($endd))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option   value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>日";
                  
?> </td>
      </tr>
      <tr> 
        <td bgcolor="#33CCFF"> 是否按时段播出 
          <input type=checkbox name=timeflag value=1 value="<? echo $row["timeflag"?>">
        </td>
        <td bgcolor="#009999"> 从<?
                
echo "<select  name=starth>";
                   for (
$i=0;$i<=23;$i++)
                   {
                         if (
$i==intval($starth))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>时";
                  echo 
" <select  name='startm'>";
                   for (
$i=0;$i<=59;$i++)
                   {
                         if (
$i==intval($startm))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option   value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>分";
                    echo 
" <select  name='starts'>";
                   for (
$i=0;$i<=59;$i++)
                   {
                 if  (
$i==intval($starts))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option   value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>秒";
                  
?>到 <?
                
echo "<select  name=endh>";
                   for (
$i=0;$i<=23;$i++)
                   {
                         if (
$i==intval($endh))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>时";
                  echo 
" <select  name='endm'>";
                   for (
$i=0;$i<=59;$i++)
                   {
                         if (
$i==intval($endm))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option   value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>分";
                    echo 
" <select  name='ends'>";
                   for (
$i=0;$i<=59;$i++)
                   {
                 if  (
$i==intval($ends))
                                     echo 
"<option selected  value='".$i."'>".$i."</option>";
                            else
                     echo 
"<option   value='".$i."'>".$i."</option>";
          }
                  echo 
"</select>秒";
                  
?> </td>
      </tr>
      <tr> 
        <td bgcolor="#33CCFF"></td>
        <td bgcolor="#009999"> 
          <div align="center">   
             <input type=hidden name=id value="<? echo $id ?>"> 
            <input type=submit name=submit value="修改"  onclick="javascript:return ckk()">
             <input type=button name=submit value="返回"  onclick="javascript:location.href='./adsbrow.php'">
          </div>
        </td>
      </tr>
    </table>
    <span class="unnamed1"></span>
</form>   
</center>
//////////////////////////////////////////////////////////////////////////////////////////////////////////
说明:
   你只要将下面的桢插入到你得高高播出叶面即可
   1)
   banner.php 要放到相应目录当然你可自己定
   2)adsbrow.php
     adsdetail.php 是后台管理,你可随便配置
   3)datebase2.php
     时数据库连接文件  
/////////////////////////////////////////////////////////////////////////////////////////////////////////
<IFRAME MARGINHEIGHT=0 MARGINWIDTH=0 FRAMEBORDER=0 width=472 height=65 SCROLLING=NO 
SRC='http://www.yoursite.com/ads/banner/banner.php?URL="+document.location+"&bannerID=1'></iframe>