Home  >  Article  >  Backend Development  >  Advertising slots randomly call products

Advertising slots randomly call products

WBOY
WBOYOriginal
2016-07-25 08:50:48987browse
Some time ago, I saw some random advertising pages on my uncle’s website, and I thought they were pretty good.
Let me share, there are mainly three parts, the function part, the advertising code file, and the final function call. The code is as follows:
  1. //Random ad display
  2. function randads(){
  3. $dir="ads"; //Set the directory where records are stored
  4. //$dir="ads"; //Set the directory where records are stored directory
  5. $ads="$dir/ads.txt"; //Set advertising code file
  6. $log ="$dir/ads.log"; //Set ip record file
  7. $ads_lines=file($ads) ;
  8. $lines=count($ads_lines);//The total number of lines in the file
  9. //Read the total number of ads $ads_count and the number of displays into the array $display_array
  10. $ads_count=0;
  11. $display_count=0;
  12. for ($i =0;$i<$lines;$i++){
  13. if((!strcmp(substr($ads_lines[$i],0,7),"display"))){
  14. $ads_count+=1;
  15. $display_array [$ads_count]=substr($ads_lines[$i],8);
  16. $display_count+=$display_array[$ads_count];
  17. }
  18. }
  19. //Decided to randomly display the serial number $display_rand
  20. srand((double)microtime() *1000000);
  21. $display_rand = rand(1,$display_count);
  22. //Determine the advertising serial number $ads_num
  23. $pricount=0;
  24. $ads_num=1;
  25. for($i=1; $i<=$ ads_count; $i++) {
  26. $pricount += $display_array[$i];
  27. if ($display_rand<=$pricount) {$ads_num=$i;break;}
  28. }
  29. //Play ads
  30. $num= 0;
  31. $flag=0;
  32. for($i=0;$i<$lines;$i++){
  33. if((!strcmp(substr($ads_lines[$i],0,7),"display "))){$num++;}
  34. if(($num==$ads_num)and($flag==0)){$flag=1;continue;}
  35. if(($flag==1)and strcmp ($ads_lines[$i][0],"#")){echo $ads_lines[$i];continue;}
  36. if(($flag==1)and(!(strcmp($ads_lines[$i] [0],"#")))){break;}
  37. }
  38. //Record the number of times the ad is displayed
  39. $fp=fopen($log,"a");
  40. fputs($fp,date( "Y-m-d H: i:s " ).getenv("REMOTE_ADDR")."==>".$ads_num."n");
  41. fclose($fp);
  42. }
  43. ?>
Copy code
  1. ########Each ad code is separated by '#', display is the display weighting number, the larger the number, the more times it will be displayed #########
  2. ### ###########################
  3. display=10
  4. Programmer is in trouble, a good gift for you!
  5. ############ ##################
  6. display=10
  7. Programmer is on the road, limited edition T-shirt!
Copy code
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn