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

首頁 > 開發 > PHP > 正文

用PHP程序實現隨機廣告圖片顯示

2024-05-04 23:05:27
字體:
來源:轉載
供稿:網友

以下為引用的內容:
<?php
  #########隨機廣告顯示##########
  function myads(){
  $dir="ads"; #設置存放記錄的目錄
  //$dir="ads"; #設置存放記錄的目錄
  $ads="$dir/ads.txt"; #設置廣告代碼文件
  $log ="$dir/ads.log"; #設置ip記錄文件
  
  $ads_lines=file($ads);
  $lines=count($ads_lines);#文件總行數
  
  ####讀出廣告總數$ads_count和顯示次數到數組$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];
   }
  }
  ####決定隨機顯示序號$display_rand#####
  srand((double)microtime()*1000000);
  $display_rand = rand(1,$display_count);
  
  ###決定廣告序號$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;}
  }
  ####紀錄廣告顯示次數#########
  $fp=fopen($log,"a");
  fputs($fp,date( "y-m-d h:i:s " ).getenv("remote_addr")."==>".$ads_num."/n");
  fclose($fp);
  }
  ?>

廣告代碼文件ads.txt

以下為引用的內容:
  ########每個廣告代碼之間用'#'隔開,display為顯示加權數,越大顯示次數越多######
  display=10   
  <a href="廣告1連接地址">
  <img src=http://chinaz.com/program/php/"/images/banner/VeVbcomad1.gif" alt="廣告1"> </a>
  ################################
  display=10   
  <a href="廣告2連接地址" target=_blank>
  <img src=http://chinaz.com/program/php/"/images/banner/VeVbcomad2.gif" width="468" height="60" alt="廣告2" border="0"></a>

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扬州市| 和硕县| 青岛市| 沂南县| 屯留县| 安陆市| 太原市| 青州市| 青田县| 金坛市| 宁武县| 理塘县| 禹城市| 外汇| 宾川县| 双桥区| 松溪县| 北川| 耒阳市| 太谷县| 冷水江市| 集安市| 西昌市| 格尔木市| 登封市| 长宁县| 宽城| 满城县| 丰镇市| 竹北市| 将乐县| 独山县| 探索| 通江县| 西畴县| 永安市| 丹巴县| 板桥市| 鄂托克旗| 彭泽县| 兴义市|