Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7248517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:08:55+00:00 2026-05-28T22:08:55+00:00

We have a small php script, that displays random adverts on our site .

  • 0

We have a small php script, that displays random adverts on our site . The banners are served from any location we designate.

What I really would like to know, or be pointed in the right direction is, can we somehow collate the number of impressions each banner gets and possibly the click count on that banner.

I am sure this can be done in php, and stored in a db. Just havent got a clue. I searched on Google, and seemingly everything I could find harks back to 2002 and 2003 lol.

Here is our script:

<?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>"; ?>

To initiate the above code ( we fire an include request )

<?php include ("../adserver/thescriptabove.php"); ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:08:56+00:00Added an answer on May 28, 2026 at 10:08 pm

    I see that you already selected an answer, so I’m not sure if you figured it all out, but I was writing out a little tutorial for you. Finally got it done, hope it still helps you out.

    Your method seems to be working fine for serving banners, but if you’re going to get into databases and tracking clicks/impressions, I would suggest that you go all in. So store your banner properties in the database as well. I’m going to get ahead and assume that your server/web host allows for a few free MySql databases.

    What you need to do is create a database, as well as a User/Admin for the database. Then you’re going to access the database with a MySql manager, most web hosts provide phpMyAdmin. Once you’re inside the database, you need to set up a table to record your data.

    Here’s how I want you to set it up:

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

    Now that you’ve got the database done, here comes the hard part, the PHP. I’ve pretty much done it for you, but it’s untested, so I’m sure there will be bugs, that you will have to work out. But it should point you in the right direction, and help you learn.

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

    Good luck

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small PHP script that is selecting a random sentence from an
I have a small PHP script that takes the values from a dropdown list
So I have a small PHP script that generates an xml feel for the
I have created a small php script locally that runs a java application in
I have a small script which im using to test PHP mail(), as below:
I have a small AJAX application, written in PHP that I did not secure
I have written a small chat app that uses mysql + php to facilitate
I have a small php script which allows me to upload image file on
I have small HTTP server script that receives HTTP client requests and replies. I
I have a small web form which will cause a PHP script to send

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.