All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
SQL Server中, DateTime (日期)型操作的 SQL语法_[SQL Server教程]
使php程序无后缀名也能解析的apache,设置_php资料_编程技术
解析SQL Server中如何得到自动编号字段_[SQL Server教程]
一些使用频率较高的非常实用的PHP函数_php资料_编程技术
HTML 初学者指南(1)_[Html教程]
手把手教你制作vs2005装饰_[Asp.Net教程]
.net(C#)开发小技巧漫谈之三:关于在Web开发中绑定数据的三种方法_[Asp.Net教程]
PHP应用技巧小总结:几个有用的小技巧_php资料_编程技术
详细讲解PHP编程中分页显示的制作_[PHP教程]
SQL中DATALENGTH,用法_mssql学习_编程技术
Reading number is top 10 pictures
含苞欲放的素颜美少女1
乳娘帕梅拉安德森2
Tie a large font of mouse
初吻给了谁?
India's national beauty of the college students
全身蕾丝丝质美臀
每天进步一点点
移民小国也实惠1
More attractive than sora aoi4
Photographed the passion of the clients and prostitutes in the sex trade picture1
Download software ranking
C#与.NET技术平台实战演练
Unix video tutorial5
圣殿祭司的ASP.NET.2.0.开发详解-使用C#
Photoshop 8.0图象编辑软件
Tram sex maniac 2 (H) rar bag8
Tram sex maniac 2 (H) rar bag10
jdk1.5
Unix video tutorial9
asp.net技术内幕
星际争霸1.08硬盘免安装版
归海一刀 published in(发表于) 2014/2/17 7:33:22 Edit(编辑)
PHP实例:用PHP实现广告轮播_[PHP教程]

PHP实例:用PHP实现广告轮播_[PHP教程]

PHP实例:用PHP实现广告轮播_[PHP教程]

网路广告,变成了 Internet 上的热门学问。而 468x60 更变成了广告人员绞尽脑汁的尺寸。
  在处理广告时,若能直接使用浏览器将广告的 468x60 图档送到处理广告的伺服器中,相信是件很舒服的事,不用再开 FTP 程式,搞大半天只为了 upload。

  这个问题,是所有 Web CGI 程式的痛,包括 ASP、Prel....等等,都需要再经过系统元件的增加才能达成。号称最强的 Web CGI 程式: PHP,在这方面的表现没有令人失望,甚至傲视其它的 CGI 工具。

  File Upload 功能在 RFC 1867 文件有有详细的说明,是利用特殊的文件格式 (content-type) multipart/form-data。值得注意的是浏览器一定要用 Netscape 3.0 以上或 MS Internet Explorer 4.0 以上的版本才能将档案上传。

  先看下面的 HTML 原始码


< form enctype="multipart/form-data" action="next.php" method=post >
您的大名: < input type=text name=user >< br >
档案名称: < input name="myfile" type="file" >< br >
< input type="submit" value="送出" >
< /form >

  在 form 的标签中,要加入 enctype="multipart/form-data" 的字串,表示使用者输入的资料上有档案上传,同时 method 一定要用 POST 而不能用 GET。

  在上面的码中,若使用者姓名填入 Wilson Peng,并选 c:\myphoto.gif 的档案,在使用者按下送出键后,浏览器则传送出下面的 POST 资料。


Content-type: multipart/form-data, boundary=AaB03x

--AaB03x
content-disposition: form-data; name="user"

Wilson Peng
--AaB03x
content-disposition: form-data; name="myfile"
Content-type: multipart/mixed, boundary=BbC04y

--BbC04y
Content-disposition: attachment; filename="myphoto.gif"
Content-type: image/gif
Content-Transfer-Encoding: binary

...myphoto.gif 内容略...
--BbC04y--
--AaB03x--


  看到上面的资料中,boundary=AaB03x 即为分开不同栏位资料的讯息,其中的 AaB03x 编码方法,视浏览器的版本不同而异,通常是浏览器杂凑产生的。之后就可以看到用 --AaB03x 来隔开不同的栏位。
  以上面为例,处理 form 的 action 程式 next.php,会主动产生四个变数,见下表

变数名 说明
myfile 即上传的档案内容
myfile_name 上传档案在使用者端的名称
myfile_size 上传档案的大小
myfile_type 上传档案的格式,如 "image/gif"


  在 next.php 程式要做的最重要动作,就是好好的使用这四个变数,否则程式一结束,使用者上传的档案就消失了。因此,要先将 myfile 复制到存放广告图的目录中

copy(banner,"/home1/biglobe3/ad/".banner_name);

  这行程式就是将档案存在 /home/htdocs/ad 的目录中,就上面的例子而言,就将档案存到 /home/htdocs/ad/myphoto.gif。重要的是,存放的目录不能是 Web Server 无法读到的目录,而应放在网站的 Homepage 所在目录中,才可以在网路上看到。

  或许程式要更细部的处理,例如比对取得的档案大小与系统回报的是否相同....等等,就可以用 myfile_size 变数了。

  若在 form 中设定 input file 的名称改掉,则在 Upload 的变数也一起改,如

< input name="upfile" type="file" >

  则变数就改成 upfile、upfile_name、upfile_size、与 upfile_type。


  因此,下面的例子就利用 File Upload 及 Oracle 7.x 后端资料库,将档案放在 Web Homepage 目录中,相关资讯则存在 Oracle 中。当然,加上使用者认证,让有帐号的使用者才能上传图片,可避免刽客 (cracker) 等将不雅或不适当的广告上传。例中有关资料库的设定和 5.4 留言版的设定相同。


< html >
< head >
< ?php
// adadd.php
if ((banner=="") and (url=="")) {
? >
< title >新增广告< /title >
< /head >
< body >
加权值数字愈大,图片出现的机率就愈高,内定值为 1。
< FORM ENCTYPE="multipart/form-data" ACTION="adadd.php" METHOD=POST >
< table border=0 >
< tr >< td align=right >广告 Banner: < /td >< td >< input name=banner TYPE="file" >< /td >< /tr >
< tr >< td align=right >广告网址 URL: < /td >< td >< input name=url type=text size=30 >< /td >< /tr >
< tr >< td align=right >辅助字串 ALT: < /td >< td >< input name=alt type=text size=30 >< /td >< /tr >
< tr >< td align=right >广告说明: < /td >< td >< input name=descript type=text size=30 >< /td >< /tr >
< tr >< td align=right >显示加权: < /td >< td >< input name=priority type=text size=5 value=1 >< /td >< /tr >
< tr >< td colspan=2 align=right >< input type="submit" VALUE="确定" >< /td >< /tr >
< /table >
< /FORM >
< ?
} else {
if (file_exists("/home/htdocs/ad/".banner_name)) {
CommonHeader("档案 ".banner_name." 已存在");
echo "< p >< br >< br >广告档案已经存在\n< p >< br >< br >< /body >< /html >";
exit;
};

copy(banner,"/home1/biglobe3/ad/".banner_name);

putenv("ORACLE_SID=WWW");
putenv("NLS_LANG=american_taiwan.zht16big5");
putenv("ORACLE_HOME=/home/oracle/product/7.3.2");
putenv("LD_LIBRARY_PATH=/home/oracle/product/7.3.2/lib");
putenv("ORA_NLS=/home/oracle/product/7.3.2/ocommon/nls/admin/data");
putenv("ORA_NLS32=/home/oracle/product/7.3.2/ocommon/nls/admin/data");

handle=ora_logon("user38@WWW","iam3849") or die;
cursor=ora_open(handle);
ora_commitoff(handle);

query="insert into ad(url, banner, alt, descript, priority) values('url', 'banner_name', 'alt', 'descript', priority)";
ora_parse(cursor, query) or die;
ora_exec(cursor);
ora_close(cursor);
ora_logoff(handle);

echo "< title >广告新增完成< /title >";
echo "< /head >";
echo "< body >";
echo "< a href=".url." >< img src=/uploads/img//200742516241885977801." alt=\"".alt."\" border=0 >< /a >< p >";
echo "< ul type=disc >";
echo "< li >广告网址: ".url;
echo "< li >辅助字串: ".alt;
echo "< li >广告说明: ".descript;
echo "< li >显示加权: ".priority;
echo "< /ul >";
}

? >
< /body >
< /html >


  当然要使用上面的程式之前别忘了先增加 ad 资料表,SQL 及栏位如下


CREATE TABLE ad (
url varchar2(1024) not null,
banner varchar2(1024) not null,
alt varchar2(255) null,
descript varchar2(255) null,
priority number(4) not null default 1
);

  序号 栏位 名称 资料形态 资料长度 栏位说明
  0 广告网址 url varchar2 1024
  1 图片路径 banner varchar2 1024
  2 字串显示 alt varchar2 255
  3 广告说明 descript varchar2 255
  4 显示加权 priority number 4 1 为内定值,0 表停用


  值得一提的是在这加入了加权的功能,若一个广告要提升曝光率,则可以将显示加权的栏位数字加大,例如 5,它的出现机率就会比只设为 1 的高五倍。


< ?php
// ad.php
putenv("ORACLE_SID=WWW");
putenv("NLS_LANG=american_taiwan.zht16big5");
putenv("ORACLE_HOME=/home/oracle/product/7.3.2");
putenv("LD_LIBRARY_PATH=/home/oracle/product/7.3.2/lib");
putenv("ORA_NLS=/home/oracle/product/7.3.2/ocommon/nls/admin/data");
putenv("ORA_NLS32=/home/oracle/product/7.3.2/ocommon/nls/admin/data");

handle=ora_logon("user38@WWW","iam3849") or die;
cursor=ora_open(handle);
ora_commitoff(handle);

query="SELECT url, banner, alt, priority FROM ad where priority > 0";
ora_parse(cursor, query) or die;
ora_exec(cursor);
i=pricount=0;
while(ora_fetch(cursor)) {
ad[i][0] = ora_getcolumn(cursor,0);
ad[i][1] = ora_getcolumn(cursor,1);
ad[i][2] = ora_getcolumn(cursor,2);
ad[i][3] = ora_getcolumn(cursor,3);
pricount = ad[i][3];
i ;
};

ora_close(cursor);
ora_logoff(handle);

srand((double)microtime()*1000000);
pri = rand(1,pricount);
pricount=0;
for(i=0; i< count(ad); i ) {
pricount = ad[i][3];
if (pri < = pricount) {
ad1[]="< a href=".ad[i][0]." target=new >< img src=/uploads/img//200742516241885977802." width=468 height=60 border=0 alt=\"".ad[i][2]."\" >< /a >";
}
}
echo ad1[0];

? >


  上面的程式为公用的广告显示程式,其中的 pricount 变数为所有广告 priority 加起来的和。程式先将所有的广告资讯读到阵列变数 ad 中,随即关上资料库。再依时间取乱数种子,之后再从 1 到 pricount 间随机取一个数字。

  网页中要用广告程式,只要在需要广告的地方加上 < ? include("ad.php"); ? > 就可以了,当然 Include 的路径 (在 httpd.conf 中) 要先设好才行。

  上面的程式还有改进空间,可以加入广告的 Click Log 功能,或是显示的 Log 功能,改动显示加权的程式....等等,就不做范例了,毕竟在这儿是要介绍 PHP 的实际应用及程式开发,而不是套件开发。真的需要现成的广告套件,不妨到 http://www.phpwizard.net/phpAds,这是一套用 PHP 开发出来的广告程式






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.