search
HomeBackend DevelopmentPHP Tutorial用php实现广告轮播_PHP

用php实现广告轮播_PHP

Jun 01, 2016 pm 12:34 PM
adcursoraccomplishadvertisefile

网路广告,变成了 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" 的字串,表示使用者输入的资料上有档案上传,同时 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 的变数也一起改,如



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


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




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


加权值数字愈大,图片出现的机率就愈高,内定值为 1。


广告 Banner:
广告网址 URL:
辅助字串 ALT:
广告说明:
显示加权:



} else {
if (file_exists("/home/htdocs/ad/".$banner_name)) {
CommonHeader("档案 ".$banner_name." 已存在");
echo "广告档案已经存在\n";
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 "广告新增完成";
echo "";
echo "";
echo "";
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("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$pricount += $ad[$i][3];
if ($pri $ad1[]="";
}
}
echo $ad1[0];

? >


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

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

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

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software