加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何计算横幅广告展示次数和点击次数

发布时间:2020-12-13 18:17:04 所属栏目:PHP教程 来源:网络整理
导读:我们有一个小的 PHP脚本,在我们的网站上显示随机广告.横幅是从我们指定的任何位置提供的. 我真正想知道的,或指向正确的方向是,我们能够以某种方式整理每个横幅获得的印象数以及该横幅上的点击次数. 我确信这可以在php中完成,并存储在db中.只是没有线索.我在G
我们有一个小的 PHP脚本,在我们的网站上显示随机广告.横幅是从我们指定的任何位置提供的.

我真正想知道的,或指向正确的方向是,我们能够以某种方式整理每个横幅获得的印象数以及该横幅上的点击次数.

我确信这可以在php中完成,并存储在db中.只是没有线索.我在Google上搜索过,似乎我能找到的所有东西都可以追溯到2002年和2003年.

这是我们的脚本:

<?php
$Img1 = "path to image folder/banner.png";
$Alt1 = "alt text for banner";
$Url1 = "http://www.externaldomain.com";

$Img2 ="path to image folder/banner.png";
$Alt2 = "alt text for banner";
$Url2 = "http://www.externaldomain.com";

$Img3 ="path to image folder/banner.png";
$Alt3 = "alt text for banner";
$Url3 = "http://www.externaldomain.com";

$Img4 ="path to image folder/banner.png";
$Alt4 = "alt text for banner";
$Url4 = "http://www.externaldomain.com";

$Img5 ="path to image folder/banner.png";
$Alt5 = "alt text for banner";
$Url5 = "http://www.externaldomain.com";

$num = rand (1,5);

$Image = ${'Img'.$num};
$Alt = ${'Alt' .$num};
$URL = ${'Url'.$num};

Print "<a href="".$URL.""><img src="".$Image."" alt="".$Alt."" /</a>"; ?>

启动上面的代码(我们触发包含请求)

<?php include ("../adserver/thescriptabove.php"); ?>

任何帮助赞赏

我看到你已经选择了一个答案,所以我不确定你是否认为这一切,但我正在为你写一个小教程.终于完成了,希望它仍然可以帮助你.

您的方法似乎适用于提供横幅广告,但如果您要进入数据库并跟踪点击次数/展示次数,我建议您全力以赴.因此,请将您的横幅广告属性存储在数据库中.我将继续前进,并假设您的服务器/ Web主机允许一些免费的MySql数据库.

您需要做的是创建一个数据库,以及数据库的用户/管理员.然后,您将使用MySql管理器访问数据库,大多数Web主机提供phpMyAdmin.进入数据库后,需要设置一个表来记录数据.

这是我想要你设置的方式:

|Table Name: Banners      |
|-------------------------|
|Field:    | Type:        |
|-------------------------|
|ID        | int(5)       | The Primary Key and Autoincrement attributes should be set on the ID field as well
|Image     | varchar(100) |
|Url       | varchar(100) |
|Caption   | varchar(100) |
|Views     | int(10)      |
|Clicks    | int(10)      |

现在您已经完成了数据库,这里有一个难点,即PHP.我已经为你做了很多,但它没有经过测试,所以我肯定会有bug,你必须要解决.但它应该指向正确的方向,并帮助你学习.

<?PHP

// First of all,we need to connect to the MySql server
// For more info,check out: http://php.net/manual/en/function.mysql-select-db.php
$conn = mysql_connect("localhost","username","password");
if(!$conn){
    die('Could not connect to the MySql Server ' . mysql_error());
}

// Now that we've connected to the MySql sever,we need to select the database
// More info can be found on the same link as above
$db_selected = mysql_select_db('my_database',$conn);
if(!$db_selected) {
    die ('Could not select the MySql Database: ' . mysql_error());
}

// Now we need to check the URL parameters to see,if we came to this page normally or because a banner was clicked
// If normally,we serve a random banner and increment the Views field in the database
// Otherwise,we increment the Clicks field and direct the user to the banner's URL


if(!isset($_GET['Clicked'])){
    // if the Clicked parameter is not set,we came to the page normally

    // Let's select a random banner from the database
    $result = mysql_query("SELECT * FROM banners ORDER BY RAND() LIMIT 1") or die(mysql_error());
    $row = mysql_fetch_array(result);   

    // Now let's increment the Views field for the banner we selected
    mysql_query("UPDATE banners SET Views = Views + 1 WHERE ID = '" . $row['ID'] . "'") or die(mysql_error());

    // let's set the URL to this same page but with the Clicked parameter set
    $url = "banner_server.php?Clicked=" . $row['ID'];

    // Last but not least,we have to output the banner HTML
    // Notice,I set the caption on both the 'alt' and 'title' attributes of the 'img' tag,// that's because IE shows the value of the 'alt' tag when an image is hovered,// whereas Firefox shows the value of the 'title' tag,just thought you might want that!
    echo "<a href="" . $url . ""><img src="" . $row['Image'] . "" alt="" . $row['Caption'] . "" title="" . $row['Caption'] . "" /></a>";

}else{
    // Otherwise,increment the Clicks field and redirect

    // First,let's get the ID of the banner that was clicked from the Clicked parameter
    $id = (int)$_GET['Clicked'];

    // now let's increment the Clicks field for the banner
    mysql_query("UPDATE banners SET Clicks = Clicks + 1 WHERE ID = '" . $id . "'") or die(mysql_error());

    // now let's select the banner from the database so we can redirect to the URL that's associated with it
    $result = mysql_query("SELECT * FROM banners WHERE ID = '" . $id . "'") or die(mysql_error());
    $row = mysql_fetch_array(result);

    // redirect to the URL
    header("location: " . $row['Url']);
}


// Close the MySql connection
mysql_close($conn);

?>

祝好运

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读