banner管理的php程序


// Based on adsetup.phtml 
// version of April 17, 1998 
// Laura DiFiore <[email protected]

// This script will update my advertiser database with information about 
// new advertisers, display ads, and update impressions and clicks 
// this uses mysql for the necessary banner ad codes 

// Summary of changes by Jeff Schmitt 
// * originally only 10 banner ads were supported -- now any number 
// 
// * added $PHP_SELF so the script could be easily renamed 
// 
// * changed "switch ($state)" and function structure to a simpler 
// "if ($state==" eliminating the global variables declarations 
// 
// * added the ability for the administrator to list the ads in one screen 
// 
// * extensively changed the ad editing interface 
// 
// * in the db definition, changed 'codenumber' to 'userid char(10)' 
// 
// * the program is not sensitive to the order of columns defined 
// in the database 
// 
// * administrator login can edit the ads without typing 
// the passwords of each individual advertiser. 
// 
// * advertiser login can view and edit that one ad only, but cannot 
// change the impressions and click statistics 
// 
// * user logins -- keep userid and password in cookie 

/************************************************** 

Date: Fri, 17 Apr 1998 23:13:42 -0600 
From: Laura DiFiore <[email protected]

... a fairly complete but basic and rudimentary system with some 
administrative functions that will allow me to add a banner to the 
rotation, update a current banner, view stats on an individual banner (or 
allow the advertiser to view their stats in real time!), track the 
impressions and the clicks ... 

Since I received about 50 emails asking me to share whatever I learned, 
I'm posting my final (hah!) script -- so here's my own little banner ad 
rotation system, for you all to criticize, compliment, edit, ignore, 
improve, or delete as desired. 

If any code experts out there would like to help me improve on this and 
especially add more features, or have ideas on improving it, let me 
know, I'll fix and update this and then when it's "pretty" and more 
efficient than the way I have it, I'll add it to the PX. 

-=-=-=-=-=- 
Installing/Using this script: 

1. Setup a mysql table with the following columns: 

create table bannerad ( 
userid char(10) primary key, 
passwd char(10), 
advertiser char(50), 
notes varchar(255), 
website varchar(255), 
imagesrc varchar(255), 
impressions mediumint unsigned, 
clicks mediumint unsigned 


If you change any of the column names, be sure to change them throughout 
the script! Column Names are hardcoded to these names. 

2. Change the variables that are listed in the beginning of the script 
to what you need them to be to access your database. Be sure to define 
a administrator name and administrator password. 

3. Open the script with your browser 

http://www.yourwebsiteaddress.com/bannerad.php3 

Login with the administrative name and password you defined in the 
script. Click on "Add an Advertiser" to setup your banner ads. 

4. To call this script from your own page place the following code 
whereever you wish the banner ads to be shown: 

$state="showad"; 
require("bannerad.php3"); 

**************************************************/ 


// SETUP YOUR SPECIFIC VARIABLES HERE!!!! 

$database"schmitt"// obviously the name of the database 
$table"bannerad"// whatever you named the bannerad table 
$dbserver ""// null string is also called 'localhost' 

// my PHP and JDBC programs use the same .mysql-init file which 
// stores the mysql db username and password 

if ($fp=fopen"/usr/faculty/schmitt/.mysql-init""r")) { 
$dbusername=chop(fgets($fp,500)); // chop -- remove trailing whitespace 
$dbpassword=chop(fgets($fp,500)); 
fclose($fp); 
} else { 
error"cannot read .mysql-init"); 


// the administrative functions are protected by the following 
// userid and password 
$adminname"schmitt"// a logon name for the administrative functions 
$adminpass"jeff"// a password for the administrative functions 

// Establish a connection with the database server 
function connect($sServer$sUser$sPass$sDB) { 
if (!(
$nConnect=mysql_connect($sServer$sUser$sPass))) { 
error"the database server is not available."); 

// and select the database 
if (!mysql_select_db($sDB$nConnect)) { 
error"database select error: $sDB is not available."); 

return 
$nConnect


// ------------------------------------------------- 

function title($title) { 
echo 

<HTML><HEAD> 
<TITLE>$title</TITLE> 
</HEAD> 
<BODY BGCOLOR="
FFFFFF"> 
<H2>$title</H2> 
"



function 
footer () { 
echo 
"</BODY></HTML>n"
exit; 


function 
error ($errorstring) { 
title"ERROR: $errorstring"); 
echo 
"Please use the browser BACK button to return to the previous page.n"
footer(); 


// not currently used 
function button($label$number) { 
global 
$USERID$PHP_SELF
?> 
<TD><FORM METHOD="POST" ACTION=" <?echo $PHP_SELF?>project.php3"> 
<INPUT TYPE="HIDDEN" NAME="USERID" VALUE=" <? echo $USERID?>"> 
<INPUT TYPE="HIDDEN" NAME="label" VALUE=" <? echo $label?>"> 
<INPUT TYPE="HIDDEN" NAME="number" VALUE=" <? echo $number?>"> 
<INPUT TYPE="SUBMIT" NAME="action" VALUE=" <? echo $label?>"> 
</FORM> 
<? 


function 
onerow($row) { 
global 
$PHP_SELF
echo 
"<TR><TD>"
echo 
$row->userid
echo 
"<TD><A HREF="$PHP_SELF?state=Show&userid=". 
"
$row->userid">"
if (
$row->advertiser) { 
echo 
$row->advertiser
} else { 
echo 
"No Name"

echo 
"</A>"
echo 
"<TD>$row->impressions<p>"
echo 
"<TD>$row->clicks<p>"
// Note: the @ suppresses the error message (divide by 0) 
@$clickpercent=(100*$row->clicks/$row->impressions); 
// problem needs to be fixed, not showing decimal places 
echo "<TD>$clickpercentn"


// this function is called for each time this script is 
// executed and a userid is needed 
// 
// The $state value "showad" and "click" do not need a userid 
// so this function is not called from there 
// 
// Note the database connection is established and this is 
// kept open as we return from this function 
// 
// If userid and password are not correct, an error 
function checklogin($userid,$passwd) { 
global 
$dbserver$dbusername$dbpassword$database$PHP_SELF
$table$UNAME$PWD$adminname$adminpass

connect($dbserver$dbusername$dbpassword$database); 

// userid and password explicitly entered (by login form) 
// take precedence over the cookie 
if ($userid!= "") { 
$UNAME=$userid
$PWD=$passwd


// remember the username and password in the cookie 
// the cookie will expire in 10 minutes (10*60) =600 seconds 
SetCookie"UNAME",$UNAME,Time()+600"/"); 
SetCookie"PWD",$PWD,Time()+600"/"); 
if (
$UNAME==$adminname && $PWD==$adminpass) { 
// successful 
return; 

$result mysql_query"SELECT * FROM $table WHERE "
"userid='$UNAME' and passwd='$PWD'"); 
if (
mysql_numrows($result)) { 
// successful 
return mysql_fetch_object($result); 


// if not successful, show error for 2 seconds and refresh 
// automatically with the login screen 
echo "<META HTTP-EQUIV="Refresh" CONTENT="2;URL=$PHP_SELF">n"
error"You must login first"); 


function 
admin() { 
global 
$dbserver$dbusername$dbpassword$database$PHP_SELF
$table
// This is the administrative functions main page 

title"Administrative Functions"); 

echo 
"<H3>Click on advertiser name for more information.</H3>n"

connect($dbserver$dbusername$dbpassword$database); 

$result=mysql_query"select * from $table"); 
// fetch the row into an 'object' which has components 
// for each of the columns of the currect row 
echo "<TABLE border cellspacing=3 cellpadding=3>n"
echo 
"<TR><TH>ID<TH>Name<TH>Impressions<TH>Clicks<TH>Click %n"
while (
$row=mysql_fetch_object($result)) { 
onerow($row); 

echo 
"</TABLE><P>n"
echo 
"<a href="$PHP_SELF?state=Add">Add an Advertiser</a>n"
footer(); 


function 
updateform() { 
global 
$userid$valueadvertiser$valuepasswd
$valuewebsite$valueimagesrc$valuenotes$PHP_SELF
?> 
Change the information for this advertiser as needed.<p> 

<FORM ACTION =" <? echo $PHP_SELF?>" METHOD = "POST"> 
<input type="hidden" name="olduserid" 
value=" <? echo $userid?>"> 

Advertiser Userid<input type="text" name="userid" 
size="10"value=" <? echo $userid?>"><p> 

Advertiser Password <input type="text" name="passwd" 
size="10"value=" <? echo $valuepasswd?>"><p> 

Advertiser Name <input type="text" name="advertiser" 
size="50" value=" <? echo $valueadvertiser?>"><p> 

Website URL Code<br> 
Input http stuff, i.e., <TT>http://www.whatever.com</TT><br> 
<input type="text" name="website" size="255" 
value=" <? echo $valuewebsite?>"><p> 

Image Code<br> 
Input THE COMPLETE code, including the &lt;IMG SRC=, WIDTH=, 
etc., stuff.<br> 
<input type="text" name="imagesrc" size="255" 
value=" <? echo $valueimagesrc?>"><p> 

Notes <input type="text" name="notes" size="255" 
value=" <? echo $valuenotes?>"><p> 

<INPUT TYPE="submit" NAME="state" VALUE="Delete"> 
<INPUT TYPE="submit" NAME="state" VALUE="Update"><p> 
</form> 
<? 


// ------------------------------------------------- 
// This code is called from webpage containing the banner ad 
// The user clicks on the ad, and the href comes here first 
// so we can update our database (number of clicks). 
// Then we send the user off to the actual webpage of the advertiser 

if ($state== "click") { 

// called when state=click 
// this will update the number of times an ad has been clicked on 
// then redirect to the advertisers URL 


connect($dbserver$dbusername$dbpassword$database); 
mysql_query"UPDATE $table SET clicks=clicks+1 WHERE "
"userid='$userid'"); 
$result=mysql_query"select website from $table where "
"userid='$userid'"); 
$website=mysql_result($result$counter"website"); 

header "Location: $websitenn"); 
exit; 


// ------------------------------------------------- 

if ($state== "Login") { 
$row=checklogin($userid,$passwd); 
if (
$UNAME==$adminname) { 
admin(); 

title"Advertiser Login"); 

echo 
"<h2>Login Successful</h2>n"
echo 
"Current Banner Ad:<br>"
echo 
$row->imagesrc
echo 
"<p>"
echo 
"<TABLE border cellspacing=3 cellpadding=3>n"
echo 
"<TR><TH>ID<TH>Name<TH>Impressions<TH>Clicks<TH>Click %n"
onerow($row); 
echo 
"</table>"
footer(); 


// ------------------------------------------------- 

if ($state== "Show") { 
checklogin""""); 

if (
$UNAME=$adminname) { 
admin(); 

$result=mysql_query"select * from $table where "
"userid='$userid'"); 

$row=mysql_fetch_object($result); 

title"Showing $userid"); 

$valueadvertiser=$row->advertiser
$valuepasswd=$row->passwd
$valuewebsite=$row->website
$valueimagesrc=htmlspecialchars($row->imagesrc); 
$valuenotes=$row->notes
updateform(); 
footer(); 


// ------------------------------------------------- 
// not currently used 

if ($state== "updateadnext") { 

/* called when state=updateadnext */ 
/* This is called by the update function, performs the actual updates, 
*/ 
/* then displays the updated advertiser information. */ 

connect($dbserver$dbusername$dbpassword$database); 

mysql_query"UPDATE $table SET advertiser='$advertiser', "
"passwd='$passwd', website='$website', imagesrc='$imagesrc', "
"notes='$notes' WHERE userid='$userid'"); 

$result=mysql_query"select * from $table where "
"userid='$userid'"); 

showobject(); 
footer(); 


// ------------------------------------------------- 

if ($state== "Delete") { 
// $olduser must be nonblank -- cannot delete a new form that is 
// being created 
if ($olduserid==$userid && $olduserid!= "") { 
connect($dbserver$dbusername$dbpassword,$database); 
title"delete - $userid"); 

mysql_query"delete from $table where userid='$userid'"); 
echo 
"$advertiser advertisement has been deleted.<p>"
echo 
"<A HREF="$PHP_SELF">OK</A>n"
footer(); 
} else { 
error"cannot delete because you are changing the userid "
"use the Update button instead."); 



// ------------------------------------------------- 

if ($state== "Update") { 
checklogin""""); 
$result=mysql_query"select * from $table where "
"userid='$userid'"); 

$row=mysql_fetch_object($result); 

if (
$olduserid== "") { 
// adding new advertiser 
if (trim($userid)== "") { 
error"userid must not be blank"); 

title"Create an advertiser"); 
connect($dbserver$dbusername$dbpassword$database); 
// Don't use this: 
// mysql_query("insert into $table values ( ... 
// since the number or order of rows may change later 
mysql_query"insert into $table (advertiser, passwd, website, "
"imagesrc, impressions, clicks, notes, userid) "
"values ('$advertiser', '$passwd','$website', '$imagesrc', "
"0, 0, '$notes', '$userid')"); 
} else if (
$olduserid==$userid) { 
// updating an existing advertiser 
title"Update an advertiser"); 
connect($dbserver$dbusername$dbpassword$database); 
mysql_query"UPDATE $table SET advertiser='$advertiser', "
"passwd='$passwd', website='$website', imagesrc='$imagesrc', "
"notes='$notes' WHERE userid='$userid'"); 
} else { 
// changing userid 
error"Sorry, I don't know how to change userids"); 

// if not successful, show error for 2 seconds and refresh 
// automatically with the login screen 
echo "<META HTTP-EQUIV="Refresh"CONTENT="2;URL=$PHP_SELF?state=Show">n"



// ------------------------------------------------- 

if ($state== "Add") { 
checklogin""""); 

title"Add an advertiser"); 
$valueadvertiser""
$valuepasswd""
$valuewebsite"http://www.somewhere.com"
$valueimagesrc=htmlspecialchars
"<img src="http://triton.towson.edu/~schmitt/bannerad/">"); 

$valuenotes""
updateform(); 
footer(); 


/* ------------------------------------------------- */ 
// not currently used 

if ($state== "Create") { 

// This takes the information from the form called in function 
// and actually adds it to the database, then shows you what you just 
// added 

connect($dbserver$dbusername$dbpassword$database); 

// determine if the userid is already in the database 
if (mysql_numrows($result=mysql_query"select * from $table where "
"userid='$userid'"))) { 
echo 
"That userid already exists<P>"
} else { 
// Don't use this: 
// mysql_query("insert into $table values ( ... 
// since the number or order of rows may change later 
mysql_query"insert into $table (advertiser, passwd, website, "
"imagesrc, impressions, clicks, notes, userid) "
"values ('$advertiser', '$passwd','$website', '$imagesrc', "
"'$impressions', '$clicks', '$notes', '$userid')"); 
$result=mysql_query"select * from $table where "
"userid='$userid'"); 


// now show the information as posted 
$row=mysql_fetch_object($result); 
echo 
"Advertiser Userid: $row->userid<P>"
echo 
"Advertiser Password: $row->passwd<P>"
echo 
"Advertiser Name: $row->advertiser<P>"
echo 
"Advertiser's Website Link $row->website<P>"
$valueimagesrc=htmlspecialchars($row->imagesrc); 
echo 
"Advertiser's Image Link: $valueimagesrc<P>"
echo 
"Advertiser's Image: $row->imagesrc<P>"
echo 
"Notes: $row->notes<P>"
footer(); 


// ------------------------------------------------- 
// This code is called from a webpage where 
// a randomly-selected banner ad is to be inserted 

if ($state== "showad") { 

// get the result set of all ad candidates 
connect($dbserver$dbusername$dbpassword$database); 

// determine how many rows are in the table 
$total_rows mysql_numrows($result=mysql_query
"select * from $table")); 

// seed the random number generator for a new random sequence 
srand(time()); 
// choose a number between 0 and total_rows 
$rnd_no=(rand()%$total_rows); 

// fetch $rnd_no objects from the database 
for ($i=0;$i<$rnd_no;$i++) { 
$row=mysql_fetch_object($result); 

// the next one is the randomly-selected banner ad 
$row=mysql_fetch_object($result); 

// show the ad code 
// the following cannot use $PHP_SELF since it is inserted 
// into another script 
echo "<a href="bannerad.php3?state=click&userid="; 
echo $row
->userid. "">"
echo 
$row->imagesrc
echo 
"</a><p>"

// update the count on the impressions 
mysql_query"UPDATE $table SET impressions=impressions+1 "
"WHERE userid='$row->userid'"); 

// do NOT exit -- 
// the banner ad may be at the head of a webpage. Since an exit 
// would stop the php interpreter, nothing after the banner ad would 
// be processed. 
// that is why there is an 'else' here and not an exit. 

} else { 

// The default page is the login page 
title"Banner Ad -- Login"); 
?> 
<H3>Welcome to the Banner Advertising system</H3> 
<FORM ACTION=" <? echo $PHP_SELF?>" METHOD = "POST"> 
Your Logon Name <input type="text" name="userid" size="10"><p> 
Your Password <input type="text" name="passwd" size="10"><p> 
<INPUT TYPE="submit" NAME="state" VALUE="Login"><P> 
<HR> 
Administrator: try logging on with <BR> 
userid: <? echo $adminname?><BR> 
password: <? echo $adminpass?> 
</html> 
<?