用PHP实现小型站点广告管理_PHP
本例在PHP4 MySQL Apache for Win9x/2000下调试通过,共包括两部分:用来增加新广告的程序AddNewAd.php3以及显示广告的程序ShowAd.php3,都存放在服务器PHP文件目录下的“test/guanggao/”目录中。在guanggao目录中建立一个存放广告链接图片的子目录“adbanner/”。下面我们按步骤进行分析。
1.首先,建立MySQL数据库:guanggao
在Windows环境下,建立一个MySQL空数据库很简单,因为每个数据库都是存在于mysql/data/目录下的一个子目录,所以只要在该目录下新建子目录guanggao就可以了,这时数据库内还没有任何表和数据。
2.建立新增广告程序:AddNewAd.php3
设计思路:其中先用表单得到用户的新广告信息;然后用PHP函数copy()将链接图片(banner)文件上传到服务器adbanner目录下;最后将图片文件名、广告网址、说明、加权信息等写入建立的MySQL数据表ad中。该程序代码如下:
# AddNewAd.php3——增添新的广告 #
增添新广告
//表单用来输入新增广告信息,并交给下面的PHP程序处理;
//其中,加权值priority数字愈大,载入页面图片出现的机率就愈高,默认为1。
//处理表单数据的PHP程序;
//图片banner名和链接地址不能为空;
if (( banner!="") & ( url!="")) {
//若广告链接和图片名已被使用,必须另选;
if (file_exists("adbanner/". banner_name)) {
echo "广告图片. banner_name.已被使用,请另选!";
exit;
};
//上传链接图片文件名到adbanner目录;
copy( banner,"adbanner/". banner_name);
//连接MySQL数据库;
connection = mysql_connect();
//选择数据源guanggao,执行查询;;
mysql_select_db("guanggao", );
query="SELECT * FROM ad";
result=mysql_query( query);
//如果数据表ad不存在,则按以下结构创建新数据表ad;
if(!result){
mysql_query("
create table ad(
url varchar(100) not null,
banner varchar(150) not null,
alt varchar(100) null,
priority tinyint(4) default 1 not null
)") or die(mysql_error());
}
//向数据表ad中插入来自于表单的新数据;
query="insert into ad(url, banner, alt, priority) values( url, banner_name,
alt, priority)";
//插入成功则显示以下信息;
try=mysql_query( query);
if( try){
echo "一条广告新增完成,详细信息:
";
echo "
";
echo "广告网址: url
广告链接说明: alt
显示加权: priority ";
}
}
?>
3.建立显示广告程序:ShowAd.php3
设计思路:得到数据库表中所有各列信息分别存到相应的一维数组中,将所有广告的权值相加得到一个最大随机数,每次载入页面用srand() 产生一个随机树种子,再用rand()函数产生一个从1到最大随机数之间的一个随机数,然后按照一定的规则随机显示不同广告条,权值越大被显示的机会越大。
# ShowAd.php3——随机显示广告 #
//连接选择数据库;
connection = mysql_connect();
mysql_select_db("guanggao", connection);
//执行查询得到广告条数;
query="SELECT url, banner, alt, priority FROM ad where priority > 0";
result=mysql_db_query("guanggao", query);
numrows=mysql_num_rows( result);
//使用mysql_fetch_object()函数获取有用的列信息并存到相应数组中;
while( row = mysql_fetch_object( result)) {
adurl[]= row->url;
adbanner[]= row->banner;
adalt[]= row->alt;
adpriority[]= row->priority;
}
//初始化中间变量;
numcheck= numrows;
i= pricount=0;
//得到最大随机数;
while( numcheck) {
pricount = adpriority[ i];
i ; numcheck--;
}
//程序执行时的百万分之一秒产生随机数种子;
srand((double)microtime()*1000000);
//得到1到最大随机数之间的一个随机数;
pri = rand(1, pricount);
//中间变量清零;
pricount=0;
//按加权值不同,产生用来显示广告的、元素为字串的数组;
for( i=0; i pricount = adpriority[ i];
if ( pri ad[]="";
}
}
//显示广告,权值越大,显示机会越大;
echo ad[0];
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to add trusted sites in Google Chrome? Some users cannot browse the web normally because the page prompts that it is not secure when surfing the Internet. At this time, they can add the website as a trusted site, and we can access it normally and surf the Internet smoothly. The editor below will bring you the steps to add a trusted site in Google Chrome. The steps are simple and easy to operate, and even beginners can easily get started. Friends who need it can learn about it together. How to add a trusted site in Google Chrome: 1. Open Google Chrome and click the three dots in the upper right corner. Click [Settings]. Choose privacy settings and security. (As shown in the picture) 2. Click Website Settings at the bottom of the page. (As shown in the picture) 3. Click on the unsafe content at the bottom of the page. (As shown in the picture) 4. Click the Add button to the right of the Allow option. (As shown in the picture) 5. Lose

How to turn off the ads recommended by 360 Browser? I believe that many users are using 360 Browser, but this browser sometimes pops up advertisements, which makes many users very distressed. Let this site carefully introduce to users how to Turn off the ads recommended by 360 Browser on your computer. How to turn off the ads recommended by 360 Browser on your computer? Method 1: 1. Open 360 Safe Browser. 2. Find the "three horizontal bars" logo in the upper right corner and click [Settings]. 3. Find [Lab] in the taskbar on the left side of the pop-up interface, and check [Enable "360 Hotspot Information" function]. Method 2: 1. First double-click

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

Users will encounter a lot of advertisements when browsing Moments, so how can they completely turn off the advertisements? Users need to click Settings in My, then find Personal Information and Permissions, then click Personalized Ads Management, and just turn off ad personalization. This article will tell you how to completely turn off ads in Moments, so take a look! WeChat usage tutorial: How to completely turn off ads in Moments? Answer: It cannot be turned off completely. You can only turn off personalized recommendations. Specific methods: 1. First, click on me, and then find the settings inside. 2. Find the personal information and permissions options. 3. Click on the personalized advertising management function inside. 4. Click the switch on the right side of ad personalization and turn it off. How to turn off ads: 1. Find the ads in Moments. 2. Click right

After updating the Edge browser, many users found that there were a lot of advertisements on the interface. For example, at the bottom of the new tab page, there were links to multiple websites and the word advertisements were marked, which looked very annoying. Is there any way to turn off personalized ads in Edge browser? The editor has searched many methods on the Internet, and I will share with you a little trick to turn off ads. How to turn off ads in the new version of Edge? 1. Open the Edge browser. 2. Click [•••] in the upper right corner. 3. Click [Settings]. 4. Click [Privacy, Search and Services]. 5. Turn off the switch on the right side of "Personalize your web experience" to turn off the personalization pushed by Microsoft.

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.
