網路廣告,變成了 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 原始碼


method=post>
您的大名:
檔案名稱:




在 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。重要的是,存放的目錄不能是 webserver 無法讀到的目錄,而應放在網站的 homepage 所在目錄中,才可以在網路上看到。

或許程式要更細部的處理,例如比對取得的檔案大小與系統回報的是否相同...,等等,就可以用 $myfile_size 變數了。

若在 form 中設定 input file 的名稱改掉,則在 upload 的變數也一起改,如



則變數就改成 $upfile、$upfile_name、$upfile_size、與 $upfile_type。

因此,下面的例子就利用 file upload 及 oraclasse 7.x 後端資料庫,將檔案放在 web homepage 目錄中,相關資訊則存在 oraclasse 中。當然,加上使用者認證,讓有帳號的使用者才能上傳圖片,可避免劊客 (cracker) 等將不雅或不適當的廣告上傳。例中有關資料庫的設定和 5.4 留言版的設定相同。





// adadd.php
if (($banner=="") and ($url=="")) {
 
?>


新增廣告


加權值數字愈大,圖片出現的機率就愈高,內定值為 1。
method=post>







廣告 banner: type="file">
廣告網址 url: type=text size=30>
輔助字串 alt: type=text size=30>
廣告說明: type=text size=30>
顯示加權: type=text size=5 value=1>



} else {
if (
file_exists("/home/htdocs/ad/".$banner_name)) {
commonheader("檔案 ".$banner_name." 已存在");
echo 
"

廣告檔案已經存在
n

";
exit;
};
 
copy($banner,"/home1/biglobe3/ad/".$banner_name);
 
putenv("oraclasse_sid=www");
putenv("nls_lang=american_taiwan.zht16big5");
putenv("oraclasse_home=/home/oraclasse/product/7.3.2");
putenv("ld_library_path=/home/oraclasse/product/7.3.2/lib");
 
putenv("ora_nls=/home/oraclasse/product/7.3.2/ocommon/nls/admin/data");
 
putenv("ora_nls32=/home/oraclasse/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_classose($cursor);
ora_logoff($handle);
 
echo 
"廣告新增完成";
echo 
"";
echo 
"";
echo 
".$url.">.$banner_name."
alt="".$alt."" border=0>

";
echo 
"";
echo 
"

  • 廣告網址: ".$url;
    echo 
    "
  • 輔助字串: ".$alt;
    echo 
    "
  • 廣告說明: ".$descript;
    echo 
    "
  • 顯示加權: ".$priority;
    echo 
    "";
    }
     
     
    ?>






  • 當然要使用上面的程式之前別忘了先增加 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 的高五倍。



    // ad.php
    putenv("oraclasse_sid=www");
    putenv("nls_lang=american_taiwan.zht16big5");
    putenv("oraclasse_home=/home/oraclasse/product/7.3.2");
    putenv("ld_library_path=/home/oraclasse/product/7.3.2/lib");
     
    putenv("ora_nls=/home/oraclasse/product/7.3.2/ocommon/nls/admin/data");
     
    putenv("ora_nls32=/home/oraclasse/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_getcbumn($cursor,0);
    $ad[$i][1] = ora_getcbumn($cursor,1);
    $ad[$i][2] = ora_getcbumn($cursor,2);
    $ad[$i][3] = ora_getcbumn($cursor,3);
    $pricount += $ad[$i][3];
    $i++;
    };
     
    ora_classose($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[]=".$ad[$i][0]." target=new> src=/ad/".$ad[$i][1]." width=468 height=60 border=0
    alt="".$ad[$i][2]."">"
    ;
    }
    }
    echo 
    $ad1[0];
     
     
    ?>




    上面的程式為公用的廣告顯示程式,其中的 $pricount 變數為所有廣告priority 加起來的和。程式先將所有的廣告資訊讀到陣列變數 $ad 中,隨即關上資料庫。再依時間取亂數種子,之後再從 1 到 $pricount 間隨機取一個數字。

    網頁中要用廣告程式,只要在需要廣告的地方加上

    ("ad.php");?>

    就可以了,當然 inclassude 的路徑 (在 htt
    arrow
    arrow
      全站熱搜

      天秤女~佳佳 發表在 痞客邦 留言(0) 人氣()