Random ads in PHP display calling code _php tutorial

Source: Internet
Author: User
Tags strcmp
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, ...

  • 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.