用PHP實(shí)現(xiàn)的隨機(jī)廣告顯示代碼
更新時(shí)間:2007年06月14日 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://www.mi222.cn>
<img src="http://www.mi222.cn/logo.gif" alt="米兒網(wǎng)絡(luò)歡迎您!"> </a>
################################
display=10
<a href=http://www.mi222.cn/dh target=_blank>
<img src="http://www.mi222.cn/dh/QQCF_Pic/logo.gif" width="120" height="60" alt="米兒網(wǎng)址導(dǎo)航,網(wǎng)站免費(fèi)登陸" 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://www.mi222.cn>
<img src="http://www.mi222.cn/logo.gif" alt="米兒網(wǎng)絡(luò)歡迎您!"> </a>
################################
display=10
<a href=http://www.mi222.cn/dh target=_blank>
<img src="http://www.mi222.cn/dh/QQCF_Pic/logo.gif" width="120" height="60" alt="米兒網(wǎng)址導(dǎo)航,網(wǎng)站免費(fèi)登陸" 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)小型站點(diǎn)廣告管理
- 用PHP實(shí)現(xiàn)小型站點(diǎn)廣告管理(修正版)
- 隨機(jī)廣告顯示(PHP函數(shù))
- php強(qiáng)制運(yùn)行廣告的方法
相關(guān)文章
深入探討"<br />"和 "\r\n" 兩者有什么
本篇文章是對"<br />"和"\r\n"兩者的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP+Ajax 檢測網(wǎng)絡(luò)是否正常實(shí)例詳解
這篇文章主要介紹了PHP+Ajax 檢測網(wǎng)絡(luò)是否正常實(shí)例詳解的相關(guān)資料,這里附有實(shí)例代碼,需要的朋友可以參考下2016-12-12基于PHP實(shí)現(xiàn)一個(gè)簡單的在線聊天功能
這篇文章主要介紹了基于PHP實(shí)現(xiàn)一個(gè)簡單的在線聊天功能,對類似功能感興趣的同學(xué),要著重看一下2021-04-04php實(shí)現(xiàn)獲取本年,本月,本周時(shí)間戳和日期格式
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)獲取本年、本月、本周時(shí)間戳和日期格式的相關(guān)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2023-12-12php獲取數(shù)組中鍵值最大數(shù)組項(xiàng)的索引值
這篇文章主要介紹了php獲取數(shù)組中鍵值最大數(shù)組項(xiàng)的索引值的方法,主要通過asort對數(shù)組進(jìn)行排序,再使用foreach循環(huán)將數(shù)組索引值賦給一個(gè)新的數(shù)組,從而獲取其對應(yīng)索引值,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03php獲取當(dāng)前時(shí)間的毫秒數(shù)的方法
php本身沒有提供返回毫秒數(shù)的函數(shù),但提供了一個(gè)microtime()函數(shù),借助此函數(shù),可以很容易定義一個(gè)返回毫秒數(shù)的函數(shù)2014-01-01PHP數(shù)組排序函數(shù)合集 以及它們之間的聯(lián)系分析
本篇文章是對PHP數(shù)組排序函數(shù)合集以及它們之間的聯(lián)系進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06