欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

利用PHP实现与ASP Banner组件相似的类

程序员文章站 2022-07-11 08:06:41
<?php
/*******************************************************
作者: 吴振亨 (biz)--phpscript
e-mail:wuzhenheng@china.com
web: http://phpscript.osh.com.cn
文件名:adrotator.class.php3                 
版权声明:                      

如果您使用了本文件,请给作者写一封e-mail   
如有问题欢迎与作者联系
谢谢您的关注!                     
********************************************************/

/*
广告条管理程序
public method
    getadvertisement($adfile)

parameters
    target=
    width=
    height=
    border=
    redirect=

*/
class adrotator {
    var $target="_blank";
    var $width="460";
    var $height="60";
    var $border=0;
    var $redirect="";
    var $bannerdata=array ();
    var $randtemp=1;

function adrotator()
    {
    }
/*
public  
返回广告的链接字串
*/
function getadvertisement($adfile)
    {
    if(count($this->bannerdata)==0)
        {
        $this->bannerdata=$this->readadfile($adfile);
        }
    $addata=$this->bannerdata;

    $uplimited=count($addata);

    //随机产生一个id
    srand((double)microtime()*1000000000000000000);
    $id=@rand(1,$uplimited);

        while($this->randtemp==$id)
               {
            srand((double)microtime()*1000000000000000000);
               //$id=@rand(1,count($addata));
               $id=@rand(1,$uplimited);
               }
           $this->randtemp=$id;

       //echo $id;
       //echo count($addata);
       //echo $addata[5]["image"];

         
        $linkstring=" <a href="".$this->redirect;
        $linkstring.="?url=".$addata[$id]["url"];
        $linkstring.="" target="".$this->target;
        $linkstring.="" title="".$addata[$id]["title"];
        $linkstring.="" >";
        $linkstring.=" <img border="".$this->border;
        $linkstring.="" src="".$addata[$id]["image"];
        $linkstring.="" width="".$this->width;
        $linkstring.="" height="".$this->height;
        $linkstring.="" >";
        $linkstring.="</a>";

        //echo $linkstring;
        return $linkstring;
    }
/*
private
读入文件的基本设置
*/
function readadfile($adfile)
    {
    $fp=fopen($adfile,"r");
    while($buffer=@fgets($fp,1024))
        {
        if(!eregi("#",$buffer))
            {
            if(eregi("^*",$buffer))
                {
                $switchto="off";
                }

            $settemp=split("=",$buffer);

            if($switchto=="")
                {
                switch     ($settemp[0])
                    {
                    case "redirect":
                    $this->redirect=$settemp[1];
                    break;

                    case "width":
                    $this->width=$settemp[1];
                    break;

                    case "height":
                    $this->height=$settemp[1];
                    break;

                    case "border":
                    $this->border=$settemp[1];
                    break;
                    case "target":
                    $this->target=$settemp[1];
                    break;
                    }
                }
            else
                {
                switch     ($settemp[0])
                    {
                    case "image":
                    $i++;
                    $bannerdata[$i]["image"]=$settemp[1];
                    //echo "<br>";echo $buffer;echo $settemp[1];
                    //echo $bannerdata[$i]["image"];
                    break;

                    case "url":
                    $bannerdata[$i]["url"]=$settemp[1];
                    break;

                    case "title":
                    $bannerdata[$i]["title"]=$settemp[1];
                    break;

                    case "rate":
                    $bannerdata[$i]["rate"]=$settemp[1];
                    break;
                    }//end of switch
                }//end of if($switch)
            }//end of if(#)

        }//end of while

    return $bannerdata;

    }//end of function

}//end of class

/*
使用方法
广告设置文件书写格式banner.txt
------开始---
redirect=./adredir.asp
width=468
height=60
border=0
*

#注释

#某某公司广告1
image=../banner/1.gif
url=http://www.asps0ite.com1
title=one
rate=30
----结束-----
说明:
与asp的广告组件相仿
*表示设置,其他为图片的设置 #:表示注释

----使用-----
$banner=new adrotator();
echo $banner->getadvertisement("./banner.txt");
echo $banner->getadvertisement("./banner.txt");
echo $banner->getadvertisement("./banner.txt");
*/
?>  

【本文版权归作者与奥索网共同拥有,如需转载,请注明作者及出处】