Reference content is as follows: <? PHP ######## 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], 0, 7), "display "))){Chinaz.com$ 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 ;} }
##### Play an advertisement ######## $ 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;} chinaz.com If ($ flag = 1) and (! (Strcmp ($ ads_lines [$ I] [0], "#") {break ;} } #### Record the ad display times ######### $ Fp = fopen ($ log, ""); Fputs ($ FP, date ("Y-m-d h: I: s "). getenv ("remote_addr "). "=> ". $ ads_num. "/N "); Fclose ($ FP ); } ?> |
Chinaz.com
Ad code file ads.txt
[Chinese webmaster site]
Reference content is as follows: ######## Each advertisement code is separated by '#'. display indicates the display weight. The larger the value is, the more times it is displayed ###### Display = 10 <A href = "ad 1 connection address"> </a> ################################ Display = 10 <A href = "Ad 2 connection address" target = _ blank> </a> |
[Chinese webmaster site]
Call <? PHP myads ();?> You can.