Home  >  Article  >  Backend Development  >  Random ad display (PHP function)_PHP tutorial

Random ad display (PHP function)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:06:40833browse

#########Random ad display##########
function myads(){
$dir="ads"; #Set storage Recorded directory
//$dir="ads"; #Set the directory where records are stored
$ads="$dir/ads.txt"; #Set advertising code file
$log ="$dir /ads.log"; #Set ip record file

$ads_lines=file($ads);
$lines=count($ads_lines);#Total number of lines in the file

## ##Read the total number of ads $ads_count and the number of displays into 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"))){
$ads_count+=1;
$display_array[$ads_count]=substr($ads_lines[$i],8); Serial number $display_rand#####
srand((double)microtime()*1000000);
$display_rand = rand(1,$display_count);

###Determine the advertising sequence 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 advertising code## #######
$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;}
}
# ###Record the number of ad display times#########
$fp=fopen($log,"a");
fputs($fp,date( "Y-m-d H:i:s " ).getenv("REMOTE_ADDR")."==>".$ads_num."n");
fclose($fp);
}
?>

advertising Code file ads.txt

########Each ad code is separated by '#', display is the display weighting number, the larger the number, the more times it will be displayed ###### ##########
###############################
display= 10


Chance2mail, a great gift for you!

##############################
display=10


Welcome to My8848 website for shopping



Call




http://www.bkjia.com/PHPjc/315413.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/315413.htmlTechArticle?php #########Random ad display########## function myads(){ $dir=ads; #Set the directory where records are stored //$dir=ads; #Set the directory where records are stored $ads=$dir/ads.txt; #Set the advertising code file...
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