PHP implementation of random ads image display

Source: Internet
Author: User
Keywords PHP implementation of random ads image display
Tags strcmp
######## #随机广告显示 ##########
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.");
Fclose ($FP);
}
?>

Ad code file Ads.txt

 ########每个广告代码之间用'#'隔开,display为显示加权数,越大显示次数越多######
 display=10  
 
 
 ################################
 display=10  
 
 
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.