This article introduces the use of TXT and PHP to implement the random advertising display call method, the principle is very simple we put the ad file in the TXT file, when there is user access, we put the TXT file loaded, and then randomly come out.
The code is as follows |
Copy Code |
######## #随机广告显示 ########## 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); } ?>
|
Ad code file Ads.txt
The code is as follows |
Copy Code |
####### #每个广告代码之间用 ' # ', display is a weighted number, the larger the number of displays ################ ################################ display=10
################################ display=10
|
Call
Can
http://www.bkjia.com/PHPjc/631561.html www.bkjia.com true http://www.bkjia.com/PHPjc/631561.html techarticle This article introduces the use of TXT and PHP to implement the random advertising display call method, the principle is very simple we put the ad file in the TXT file, when there is a user access to our TXT file loading, ...