The following are the referenced contents: <?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 and display count to array $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); } ?> |
Advertising code file Ads.txt
The following are the referenced contents: ####### #每个广告代码之间用 ' # ' is separated, display displays the weighted number, the larger the display number ###### display=10 <a href= "AD 1 connection Address" > </a> ################################ display=10 <a href= "Ad 2 connection Address" target=_blank> </a> |
Call <?php myads ();? > Can