国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > PHP > 正文

PHP實(shí)例:用PHP程序?qū)崿F(xiàn)隨機(jī)廣告圖片顯示

2024-05-04 23:03:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

<?php
  #########隨機(jī)廣告顯示##########
  function myads(){
  $dir="ads"; #設(shè)置存放記錄的目錄
  //$dir="ads"; #設(shè)置存放記錄的目錄
  $ads="$dir/ads.txt"; #設(shè)置廣告代碼文件
  $log ="$dir/ads.log"; #設(shè)置ip記錄文件
  
  $ads_lines=file($ads);
  $lines=count($ads_lines);#文件總行數(shù)
  
  ####讀出廣告總數(shù)$ads_count和顯示次數(shù)到數(shù)組$display_array########
  $ads_count=0;
  $display_count=0;
  for ($i=0;$i<$lines;$i++){
   if((!strcmp(substr($ads_lines[$i],0,7),"display"))){
   $ads_count+=1;
   $display_array[$ads_count]=substr($ads_lines[$i],8);
   $display_count+=$display_array[$ads_count];
   }
  }
  ####決定隨機(jī)顯示序號(hào)$display_rand#####
  srand((double)microtime()*1000000);
  $display_rand = rand(1,$display_count);
  
  ###決定廣告序號(hào)$ads_num######
  $pricount=0;
  $ads_num=1;
  for($i=1; $i<=$ads_count; $i++) {
   $pricount += $display_array[$i];
   if ($display_rand<=$pricount) {$ads_num=$i;break;}
  }
  
  #####播放廣告########
  $num=0;
  $flag=0;
  
  for($i=0;$i<$lines;$i++){
   if((!strcmp(substr($ads_lines[$i],0,7),"display"))){$num++;}
   if(($num==$ads_num)and($flag==0)){$flag=1;continue;}
   if(($flag==1)and strcmp($ads_lines[$i][0],"#")){echo $ads_lines[$i];continue;}
   if(($flag==1)and(!(strcmp($ads_lines[$i][0],"#")))){break;}
  }
  ####紀(jì)錄廣告顯示次數(shù)#########
  $fp=fopen($log,"a");
  fputs($fp,date( "y-m-d h:i:s " ).getenv("remote_addr")."==>".$ads_num."/n");
  fclose($fp);
  }
  ?>

  廣告代碼文件ads.txt

  ########每個(gè)廣告代碼之間用'#'隔開,display為顯示加權(quán)數(shù),越大顯示次數(shù)越多######
  display=10   
  <a href="廣告1連接地址">
  <img src="http://www.survivalescaperooms.com/images/banner/VeVbcomad1.gif" alt="廣告1"> </a>
  ################################
  display=10   
  <a href="廣告2連接地址" target=_blank>
  <img src="http://www.survivalescaperooms.com/images/banner/VeVbcomad2.gif" width="468" height="60" alt="廣告2" border="0"></a>

  調(diào)用<?php myads();?>即可


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 彭山县| 黄石市| 通榆县| 枣强县| 马公市| 清丰县| 阿瓦提县| 离岛区| 米易县| 杂多县| 黄龙县| 镇赉县| 攀枝花市| 合江县| 中方县| 民勤县| 若尔盖县| 枞阳县| 商洛市| 兴安盟| 遵义市| 罗甸县| 调兵山市| 宾川县| 疏勒县| 丰镇市| 镇江市| 浑源县| 德州市| 黄梅县| 霸州市| 丹寨县| 枣庄市| 承德县| 资中县| 定州市| 邵阳县| 富顺县| 含山县| 怀化市| 云龙县|