随机广告显示(PHP函数)


Posted in PHP onOctober 09, 2006

<?php
#########随机广告显示##########  
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和显示次数到数组$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);
}
?>

广告代码文件ads.txt

########每个广告代码之间用'#'隔开,display为显示加权数,越大显示次数越多################
################################
display=10

<a href="http://china.chance2mail.com/cgi-bin/regstep1.cgi?myid=CC00099599">  
<img src="http://china.chance2mail.com/images/banner/c_banner.gif" alt="Chance2mail,好礼物送给您!"> </a>
################################
display=10

<a href="http://www.my8848.net/agtunion/agtredirect.asp?act=homepage&agtid=2986" target=_blank>
<img src="http://www.free868.com/~xinyi/ads/8848.gif" width="468" height="60" alt="欢迎到My8848网站购物" border="0"></a>

调用<?php myads();?>即可

PHP 相关文章推荐
学习discuz php 引入文件的方法DISCUZ_ROOT
Jun 21 PHP
两个开源的Php输出Excel文件类
Feb 08 PHP
IP138 IP地址查询小偷实现代码
Feb 15 PHP
PHP 登录记住密码实现思路
May 07 PHP
使用迭代器 遍历文件信息的详解
Jun 08 PHP
php记录代码执行时间(实现代码)
Jul 05 PHP
PHP邮件发送类PHPMailer用法实例详解
Sep 22 PHP
如何在旧的PHP系统中使用PHP 5.3之后的库
Dec 02 PHP
php检测mysql表是否存在的方法小结
Jul 20 PHP
Kindeditor编辑器添加图片上传水印功能(php代码)
Aug 03 PHP
PHP删除数组中指定下标的元素方法
Feb 03 PHP
thinkPHP5.1框架使用SemanticUI实现分页功能示例
Aug 03 PHP
PHP 和 MySQL 基础教程(二)
Oct 09 #PHP
最省空间的计数器
Oct 09 #PHP
域名查询代码公布
Oct 09 #PHP
无数据库的详细域名查询程序PHP版(3)
Oct 09 #PHP
浅谈PHP语法(1)
Oct 09 #PHP
PHP 和 MySQL 基础教程(一)
Oct 09 #PHP
用文本作数据处理
Oct 09 #PHP
You might like
一个php作的文本留言本的例子(五)
2006/10/09 PHP
社区(php&amp;&amp;mysql)二
2006/10/09 PHP
php获取url字符串截取路径的文件名和扩展名的函数
2010/01/22 PHP
PHP 图片上传代码
2011/09/13 PHP
javascript 复杂的嵌套环境中输出单引号和双引号
2009/05/26 Javascript
JavaScript中的函数重载深入理解
2014/08/04 Javascript
JavaScript之数组(Array)详解
2015/04/01 Javascript
javascript中alert()与console.log()的区别
2015/08/26 Javascript
JavaScript中的各种操作符使用总结
2016/05/26 Javascript
使用bootstrap typeahead插件实现输入框自动补全之问题及解决办法
2016/07/07 Javascript
JavaScript仿支付宝6位数字密码输入框
2016/12/29 Javascript
jQuery Validation Engine验证控件调用外部函数验证的方法
2017/01/18 Javascript
JS代码检查工具ESLint介绍与使用方法
2020/02/04 Javascript
JS通用方法触发点击事件代码实例
2020/02/17 Javascript
js实现列表按字母排序
2020/08/11 Javascript
vue祖孙组件之间的数据传递案例
2020/12/07 Vue.js
[03:51]吞吞映像 每周精彩击杀top10第二弹
2014/06/25 DOTA
在Python的Django框架中更新数据库数据的方法
2015/07/17 Python
Python实现监控Nginx配置文件的不同并发送邮件报警功能示例
2019/02/26 Python
python异步编程 使用yield from过程解析
2019/09/25 Python
matplotlib 三维图表绘制方法简介
2020/09/20 Python
python爬取2021猫眼票房字体加密实例
2021/02/19 Python
宝塔面板出现“open_basedir restriction in effect. ”的解决方法
2021/03/14 PHP
button在IE6/7下的黑边去除方案
2012/12/24 HTML / CSS
理肤泉俄罗斯官网:La Roche-Posay俄罗斯
2018/07/24 全球购物
美国高端牛仔品牌:Silver Jeans
2019/12/12 全球购物
Monica Vinader官网:英国轻奢珠宝品牌
2020/02/05 全球购物
医学生求职自荐信
2013/10/25 职场文书
结构工程个人自荐信范文
2013/11/30 职场文书
2014年消防工作实施方案
2014/02/20 职场文书
元旦文艺汇演主持词
2014/03/26 职场文书
公安机关查摆剖析材料
2014/10/10 职场文书
2015年治庸问责工作总结
2015/07/27 职场文书
Python数据类型最全知识总结
2021/05/31 Python
Redis的字符串是如何实现的
2021/10/24 Redis
Python 第三方库 openpyxl 的安装过程
2022/12/24 Python