Implement advertisement carousel with php

Source: Internet
Author: User
Tags cgi web
Online advertising has become a hot topic on the Internet. The 468x60 has become the size for advertisers. When processing an advertisement, if you can directly use a browser to send the 468x60 image file of the advertisement to the server that processes the advertisement, I believe it is very comfortable. you do not need to open another FTP program, it takes up to half a day only for upload. This problem is that all Web CGI Web advertisements have become a hot topic on the Internet. The size of 468x60 has become the size of the ad staff's brains.
When processing an advertisement, if you can directly use a browser to send the 468x60 image file of the advertisement to the server that processes the advertisement, I believe it is very comfortable and you do not need to open an FTP program, it takes up to half a day only for upload.

This problem is caused by the pain of all Web CGI programs, including ASP, Prel... and so on, which must be achieved by adding system components. PHP, the most powerful Web CGI program, is not disappointing in this regard, or even proud of other CGI tools.

The File Upload function is described in detail in the RFC 1867 File. it uses the special File format (content-type) multipart/form-data. It is worth noting that the browser must use versions later than Netscape 3.0 or MS Internet Explorer 4.0 to upload files.

First look at the following HTML original 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">
</Form>

To add the enctype = "multipart/form-data" string to the form tag, it indicates that the user has uploaded files on the data input. at the same time, the method must use POST instead of GET.

In the above code, if the user name is set to Wilson Peng and c: \ myphoto.gif is selected, after the user presses the send key, the browser sends the following POST information.


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...
-- BbC04y --
-- AaB03x --


As shown in the preceding document, boundary = AaB03x is the message for separating data from different columns. the AaB03x encoding method varies with the browser version, which is usually generated by the browser. Then we can see that different columns are separated by -- AaB03x.
For example, the action program next. php for processing form automatically generates four variables, as shown in the following table.

Variable name description
$ Myfile: the uploaded file content
$ Myfile_name name of the uploaded file on the user end
$ Myfile_size size of the uploaded file
$ Myfile_type: format of the file to be uploaded, for example, "image/gif"


The most important action to do in the next. php program is to make good use of these four variables. otherwise, when the program ends, the user's uploaded files will disappear. Therefore, you need to copy $ myfile to the directory where the ad map is stored.

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

This line of code stores the file in the/home/htdocs/ad directory. in the preceding example, the file is saved to/home/htdocs/ad/myphoto.gif. It is important that the stored directory cannot be a directory that the Web Server cannot read, but should be placed in the directory where the website's Homepage is located to see it on the network.

You can use the $ myfile_size variable to process the program in more detail, for example, compare the file size obtained with the system return.

If you change the input file name in form, the variables in Upload are also changed, as shown in figure

<Input name = "upfile" type = "file">

The variable is changed to $ upfile, $ upfile_name, $ upfile_size, and $ upfile_type.


Therefore, the following example uses the File Upload and Oracle 7.x back-end databases to store the files in the Web Homepage directory. the relevant information is stored in Oracle. Of course, with user authentication, users with accounts can upload images, which can prevent hackers (cracker) from uploading indecent or inappropriate advertisements. In this example, the settings for the database are the same as those for message 5.4.


<Html>
<Head>
<? Php
// Adadd. php
If ($ banner = "") and ($ url = "")){
? >
<Title> add advertisement </title>
</Head>
<Body>
The larger the number of weighted values, the higher the probability of image appearance. The value is 1.
<Form enctype = "multipart/form-data" ACTION = "adadd. php" METHOD = POST>
<Table border = 0>
<Tr> <td align = right> advertisement Banner: </td> <input name = banner TYPE = "file"> </td> </tr>
<Tr> <td align = right> advertisement URL: </td> <input name = url type = text size = 30> </td> </tr>
<Tr> <td align = right> secondary string ALT: </td> <input name = alt type = text size = 30> </td> </tr>
<Tr> <td align = right> advertisement description: </td> <input name = descript type = text size = 30> </td> </tr>
<Tr> <td align = right> Display weighting: </td> <input name = priority type = text size = 5 value = 1> </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 ("file". $ banner_name. "already exists ");
Echo "<p> <br> The ad file already exists \ n <p> <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 ', $ priority )";
Ora_parse ($ cursor, $ query) or die;
Ora_exec ($ cursor );
Ora_close ($ cursor );
Ora_logoff ($ handle );

Echo "<title> Ad added </title> ";
Echo "Echo "<body> ";
Echo "<a href = ". $ url. "> </a> <p> ";
Echo "<ul type = disc> ";
Echo "<li> advertisement url:". $ url;
Echo "<li> secondary string:". $ alt;
Echo "<li> advertisement description:". $ descript;
Echo "<li> Display weighting:". $ priority;
Echo "</ul> ";
}

? >
</Body>
</Html>


Before using the above program, do not forget to add the ad data table. the SQL and column are 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
);

Column name of serial number
0. advertisement url: varchar2 1024
1. image path: banner varchar2 1024
2 alt varchar2 255
3. advertisement description: descript varchar2 255
4. the weighted priority number 4 1 is the inner value. table 0 is disabled.


It is worth mentioning that the weighted function is added here. if an advertisement wants to increase its exposure, it can increase the weighted column number, for example, 5, its probability of appearance is five times higher than that set to 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> </a> ";
}
}
Echo $ ad1 [0];

? >


The above code is a public ad display program, where the $ pricount variable is the sum of all ad priority values. The program first reads all the ad information into the array variable $ ad, and then closes the database. Then the random number of seeds is obtained based on time, and then a random number is obtained from 1 to $ pricount.

You only need to add the <? Include ("ad. php ");? >. Of course, the Include path (in httpd. conf) must be set first.

The above program also has room for improvement. you can add the ad's Click Log function, or display the Log function to change the display weighted program .... and so on. after all, we will introduce the actual application and program development of PHP, rather than kit development. If you really need a ready-made ad suite, go to http://www.phpwizard.net/phpads. this is an advertisement program developed by PHP.

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.