隨機(jī)廣告顯示(PHP函數(shù))
更新時(shí)間:2006年10月09日 00:00:00 作者:
<?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="http://china.chance2mail.com/cgi-bin/regstep1.cgi?myid=CC00099599">
<img src="http://china.chance2mail.com/images/banner/c_banner.gif" alt="Chance2mail,好禮物送給您!"> </a>
################################
display=10
<a href="http://www.my8848.net/agtunion/agtredirect.asp?act=homepage&agtid=2986" target=_blank>
<img src="http://www.free868.com/~xinyi/ads/8848.gif" width="468" height="60" alt="歡迎到My8848網(wǎng)站購物" border="0"></a>
調(diào)用<?php myads();?>即可
#########隨機(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="http://china.chance2mail.com/cgi-bin/regstep1.cgi?myid=CC00099599">
<img src="http://china.chance2mail.com/images/banner/c_banner.gif" alt="Chance2mail,好禮物送給您!"> </a>
################################
display=10
<a href="http://www.my8848.net/agtunion/agtredirect.asp?act=homepage&agtid=2986" target=_blank>
<img src="http://www.free868.com/~xinyi/ads/8848.gif" width="468" height="60" alt="歡迎到My8848網(wǎng)站購物" border="0"></a>
調(diào)用<?php myads();?>即可
您可能感興趣的文章:
- 強(qiáng)制PHP命令行腳本單進(jìn)程運(yùn)行的方法
- PHP運(yùn)行時(shí)強(qiáng)制顯示出錯(cuò)信息的代碼
- php廣告加載類用法實(shí)例
- 適用于抽獎(jiǎng)程序、隨機(jī)廣告的PHP概率算法實(shí)例
- php 廣告調(diào)用類代碼(支持Flash調(diào)用)
- php 隨機(jī)排序廣告的實(shí)現(xiàn)代碼
- 用PHP實(shí)現(xiàn)的隨機(jī)廣告顯示代碼
- 用PHP實(shí)現(xiàn)小型站點(diǎn)廣告管理
- 用PHP實(shí)現(xiàn)小型站點(diǎn)廣告管理(修正版)
- php強(qiáng)制運(yùn)行廣告的方法
相關(guān)文章
PHP simplexml_load_file()函數(shù)講解
今天小編就為大家分享一篇關(guān)于PHP simplexml_load_file()函數(shù)講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02PHP 存取 MySQL 數(shù)據(jù)庫的一個(gè)例子
PHP 存取 MySQL 數(shù)據(jù)庫的一個(gè)例子...2006-10-10基于數(shù)據(jù)庫的在線人數(shù),日訪問量等統(tǒng)計(jì)
基于數(shù)據(jù)庫的在線人數(shù),日訪問量等統(tǒng)計(jì)...2006-10-10php in_array 函數(shù)使用說明與in_array需要注意的地方說明
in_array函數(shù)用功能就是檢查是否存在一個(gè)值的數(shù)組 ,就是判斷當(dāng)前數(shù)組中是否存在一個(gè)指定的值哦。2010-04-04