Random ad display (PHP function)

Source: Internet
Author: User
? Php ######### random ad display ########## functionmyads () {$ dir = "ads "; # set the directory for storing records // $ dir = "ads"; # set the directory for storing records $ ads = "$ dir/ads.txt "; # set the advertisement code file $ log = "$ dir/ads. log "; # set ip record file $ ######## Random ad display ##########
Function myads (){
$ Dir = 'ads'; # set the directory for storing records
// $ Dir = 'ads '; # set the directory for storing records
$ Ads = '$ dir/ads.txt'; # set the ad code file
$ Log = '$ dir/ads. log'; # set the ip record file

$ Ads_lines = file ($ ads );
$ Lines = count ($ ads_lines); # total number of file lines

#### Total number of ad reads $ ads_count and display times to the array $ display_array ########
$ Ads_count = 0;
$ Display_count = 0;
For ($ I = 0; $ I <$ lines; $ I ){
If ((! Strcmp (substr ($ ads_lines [$ I],), 'display '))){
$ Ads_count = 1;
$ Display_array [$ ads_count] = substr ($ ads_lines [$ I], 8 );
$ Display_count = $ display_array [$ ads_count];
}
}
#### Determine the random display sequence number $ display_rand #####
Srand (double) microtime () * 1000000 );
$ Display_rand = rand (1, $ display_count );

### Determine the ad serial number $ 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 ;}
}

##### Playback advertisement code #########
$ Num = 0;
$ Flag = 0;

For ($ I = 0; $ I <$ lines; $ I ){
If ((! Strcmp (substr ($ ads_lines [$ I],), '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 ;}
}
#### Record the ad display times #########
$ 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

######## Each advertisement code is separated by '#'. display indicates the display weight, the larger the display frequency ################
################################
Display = 10



################################
Display = 10





Call You can.

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.