国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

一個廣告輪播系統(tǒng)的例子(內含文件上傳的方法)

2024-05-04 23:02:00
字體:
來源:轉載
供稿:網友
網路廣告,變成了 internet 上的熱門學問。而 468x60 更變成了廣告人員絞盡腦汁的尺寸。
在處理廣告時,若能直接使用瀏覽器將廣告的 468x60 圖檔送到處理廣告的伺服器中,相信是件很舒服的事,不用再開 ftp 程式,搞大半天只為了 upload。

這個問題,是所有 web cgi 程式的痛,包括 asp、prel....等等,都需要再經過系統(tǒng)元件的增加才能達成。號稱最強的 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。重要的是,存放的目錄不能是 webserver 無法讀到的目錄,而應放在網站的 homepage 所在目錄中,才可以在網路上看到。

或許程式要更細部的處理,例如比對取得的檔案大小與系統(tǒng)回報的是否相同...,等等,就可以用 $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("[email protected]","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
);

序號 欄位 名稱 資料形態(tài) 資料長度 欄位說明
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("[email protected]","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 的實際應用及程式開發(fā),而不是套件開發(fā)。真的需要現成的廣告套件,不妨到http://www.phpwizard.net/phpads,這是一套用 php 開發(fā)出來的廣告程式。 (摘自ccu新聞組,本來出處可能是臺灣出版的一本關于php的書:php寶典)
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 安丘市| 青神县| 清涧县| 绥德县| 城市| 肥西县| 灌阳县| 德钦县| 酒泉市| 苏尼特右旗| 乐都县| 安陆市| 临泉县| 中宁县| 开阳县| 乌恰县| 新民市| 衢州市| 公主岭市| 三河市| 丽江市| 玉屏| 乐平市| 石首市| 青海省| 获嘉县| 郯城县| 咸宁市| 黄骅市| 华安县| 平顶山市| 防城港市| 合阳县| 益阳市| 繁昌县| 房产| 建瓯市| 静安区| 苏尼特右旗| 萝北县| 永安市|