网络广告,变成了 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=/ad/" . $banner_name . " 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=/ad/" . $ad [ $i ][ 1 ]. " 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 开发出来的广告程序。