Publishing System Settings Logout Login Register
Create an Interstitial ad.
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on February 5th, 2007
8034 views
PHP Coding
Well, today, I'm going to teach you how to implement one of those ad systems that interupts you while you are happily crusing a site. This script is more like the one found on torrentz.com or break.com that are from adbrite, rather than the one found on deviant art.



Ok, here is the code:

advert.html
<html>
<head>
<title>My Site</title>
<script type="text/javascript" src="advert.php">
</script>
</head>
<body>
This is my very cool site. Enjoy your stay.
</body>
</html>


Alright, this is just a basic page. This is the page that the user will go to. You can have whatever kind of content you want. But take note of the script in the head. It calls the php file for the javascript. Simply putting that code on any page will allow you to display the ad.

advert.php
<?php

$cookie = $HTTP_COOKIE_VARS["_advert"];

if ($cookie == "false"){
  exit;
}

setcookie("_advert", "false", time()+86400);
echo <<<END

    function ad() {
    document.write('ADVERT HERE <a href="javascript:window.location.reload();">Click here to get rid of advert</a>');
    document.close();
    }
    window.onload = ad;

END;

?>


A bit more explaining invovled with this one. As you can see, we start php. This next line of code sets up the variable "cookie" and adds the cookie data to it. The name of the cookie is _advert. If this cookie is not set, the variable will be empty.

We then check to see if "cookie" is equal to false, which means we shouldn't show the advertisement, they've already seen it today. If it is, we exit and the rest of the script is not executed.

If it wasn't set to false, it apparently hasn't been set and they haven't seen the ad. We will then set the cookie. This must be done before anything is echoed or it will not work. The first thing is the name of the cookie, followed by it's value and when it will expire. It will expire in one day. It's measured in seconds and there are 86400 seconds in one day.

Now we echo the javascript that is used to show the ad. I will use the paragraph echo for this. The final END; cannot have any whitespaces on the line.

I will explain the javascript too.

    function ad() {
    document.write('ADVERT HERE <a href="javascript:window.location.reload();">Click here to get rid of advert</a>');
    document.close();
    }
    window.onload = ad;


First, we start a function and call it ad. Inside this function, we put a document.write, which writes stuff to the page. You can add some html and an iframe for your sponsor here. Then we use document.close so the page doesn't keep loading. Next, we end the function. Then, once the page is fully loaded, we execute the function. This works because when you call a document.write once the page is loaded, all of the content will be replaces by what's in the document.write.

Now obviously, this code is simple and although it can be used as it, you might want to change it around to suit your needs and do some check with the cookies. And if this tutorial starts to make no sense, it's because I wrote this at 5:00 AM.
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
jbog91

I'll talk to ya anytime about soccer!!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top