Using PHP to implement AD carousel

Source: Internet
Author: User
Tags add end file upload variables php and string version variable
Advertising network advertising has become a popular learning on the internet. And 468x60 became the size of the advertiser's brains.
In the process of advertising, if you can directly use the browser to the 468x60 map files to deal with ads in the server, I believe it is a very comfortable thing, do not have to open the FTP program, big Half-day only to upload.

This problem is the pain of all Web CGI programs, including ASP, Prel ... And so on, all need to be added to the system components to achieve. The most powerful Web CGI program: PHP, in this respect, did not disappoint, or even disdain for other CGI tools.

The file Upload feature is described in detail in RFC 1867 files, using a special file format (content-type) multipart/form-data. It is worth noting that browsers must use more than 3.0 of Netscape or MS Internet Explorer more than 4.0 version to upload files.

First look at the following HTML source code


< form enctype= "Multipart/form-data" action= "next.php" Method=post >
Your name: < input type=text Name=user >< br >
File name: < input name= "myfile" type= "file" >< br >
< input type= "submit" value= "Send Out" >
</form >

In the form label, to join the enctype= "Multipart/form-data" string, indicating that the user input data on the file upload, and method must use POST and can not use get.

In the above code, if the user's name is filled in the Wilson Peng, and select the C:\myphoto.gif file, the user presses the send out key, the browser will send the following post data.


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 content slightly ...
--bbc04y--
--aab03x--


In the above, boundary=aab03x is a message that separates different field data, where the aab03x encoding method varies depending on the browser's version, usually generated by a browser hash. You can then see the--aab03x to separate the fields.
For example, the action program to handle the form next.php, will actively generate four variables, see the following table

Description of variable name
$myfile the contents of the file uploaded
$myfile _name The name of the uploaded file on the user side
The size of the file uploaded $myfile _size
$myfile _type upload file format, such as "Image/gif"


In the next.php program to do the most important action is to use these four variables, or the end of the program, the user uploaded files disappeared. Therefore, to first copy the $myfile to the directory where the advertisement map is stored

Copy ($banner, "/home1/biglobe3/ad/". $banner _name);

This line of procedure is to put the file in the/home/htdocs/ad directory, in the above example, to save the file to/home/htdocs/ad/myphoto.gif. The important thing is that the directory you are hosting cannot be a directory that Web Server cannot read, but should be placed in the homepage directory of your site before you can see it on the web.

Perhaps the program needs to be handled more detail, for example, than the size of the file obtained and the system return. Wait, you can use the $myfile _size variables.

If you set the name of the input file in the form to change, the variables in the Upload are also changed together, as

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

The variables are changed into $upfile, $upfile _name, $upfile _size, and $upfile _type.
As a result, the following example uses the file Upload and Oracle 7.x back-end repositories to place files in the Web homepage directory and related information in Oracle. Of course, plus user authentication, so that users can upload the account number of images, can avoid executioner (cracker), such as indecent or inappropriate advertising upload. For example, the settings for the database are the same as the 5.4 message version.


< HTML >
< head >
< PHP
adadd.php
if ($banner = = "") and ($url = = "")) {
? >
< title > New Advertising </title >
< BODY >
The larger the number of weighted values, the higher the probability of the picture appearing, the default value is 1.
< FORM enctype= "Multipart/form-data" action= "adadd.php" Method=post >
< table border=0 >
< TR >< TD align=right > Advertising Banner: </td >< td >< input Name=banner type= "file" ></td &G t;</tr >
< TR >< td align=right > Advertising URL: </td >< td >< input Name=url type=text size=30 ></td ></tr >
< TR >< TD align=right > Auxiliary String ALT: </td >< td >< input Name=alt type=text size=30 ></td ></tr >
< TR >< TD align=right > Advertisement Description: </td >< td >< input Name=descript type=text size=30 ></td ></tr >
< TR >< TD Align=right > Display weighting: </td >< td >< input name=priority type=text size=5 value=1 >& Lt /td ></tr >
< TR >< TD colspan=2 Align=right >< Input type= "Submit" value= "OK" ></td ></tr >
</table >
</form >
<?
} else {
if (file_exists ("/home/htdocs/ad/". $banner _name)) {
Commonheader ("Archives", $banner _name. "Existing");
echo "< p >< br >< br > AD archive already exists \n< p >< br >< br ></body >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 ', $priorit Y) ";
Ora_parse ($cursor, $query) or die;
Ora_exec ($cursor);
Ora_close ($cursor);
Ora_logoff ($handle);

echo "< title > Advertising new Completion </title >";
echo "echo "< body >";
echo "< a href=". $url. ">< img src=/ad/". $banner _name. "Alt=\". $alt. " \ "Border=0 >< a >< p >";
echo "< ul type=disc >";
echo "< li > Advertising url:". $url;
echo "< li > Auxiliary string:". $alt;
echo "< li > Advertisement description:". $descript;
echo "< li > Display weighted:". $priority;
echo "</ul >";
}

? >
</body >
Of course, before using the above program, don't forget to add the ad table, SQL and fields as follows


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
);

Ordinal field name Data shape Data Length field description
0 Advertising URL varchar2 1024
1 Picture path Banner VARCHAR2 1024
2 string display alt VARCHAR2 255
3 Advertising Description Descript VARCHAR2 255
4 Display weighted priority number 4 1 as the default, 0 table deactivate


It is worth mentioning that in this added weighted function, if an advertisement to increase the exposure rate, you can display a weighted field number, such as 5, it will be more than 1 of the probability of five times times higher.


< 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) {
$ad 1[]= "< 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 $ad 1[0];

? >


The above program is the public advertisement display program, in which the $pricount variables for all advertising priority add up and. The program first reads all the advertising information into the array variable $ad and closes the database. Then the random number of seeds in time, and then from 1 to $pricount randomly take a number.

Web page to use the advertising program, as long as the need for advertising in the place to add <? Include ("ad.php");? > is OK, of course, the path of Include (in httpd.conf) must be set first.

The above program also has the improvement space, may add the Advertisement Click log function, or displays the log function, changes displays the weighted program .... Wait, no paradigm, after all, here is to introduce the actual application of PHP and program development, rather than suite development. Really need off-the-shelf advertising kits, as well as to Http://www.phpwizard.net/phpAds, this is a set of PHP developed with the advertising program.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.