Location: PHPKode > scripts > Pop under class > example.php
<?php
/************************************************************* 
 * This script is developed by Arturs Sosins aka ar2rsawseen, http://webcodingeasy.com 
 * Fee free to distribute and modify code, but keep reference to its creator 
 * 
 * Pop under class allows you to implement pop under advertising to your website.
 * You can customize probability of ads to appear, time for how long to show advertisement and much more.
 * Language and template files included to provide full outlook customization
 * 
 * For more information, examples and online documentation visit:  
 * http://webcodingeasy.com/PHP-classes/Implement-pop-under-advertising-to-your-site-using-pop-under-class
**************************************************************/

//just for emulating site navigation from one link to another
if(!isset($_GET['id']))
{
	$_GET['id'] = 0;
}

//declaring class instance
include("./pop_under.class.php");
$popunder = new pop_under();

/*******************************
Setting timer how long should user view advertisment
in seconds.
Default value is 10 seconds
*******************************/
$popunder->set_timer(15);

/*******************************
Setting probability for popunder to appear
in percents. 10 means there is a 10% chance
of advertisment to appear on every page view
(excluding sittuation when user is marked).
It means popunder should appear approximately
on every 10th page view, but thats random, 
so don't count on consistency
Default value is 10.
*******************************/
$popunder->set_probability(50);

/*******************************
set_exit method allows you to give visitor option
to skip advertisment and continue to requestet page
If set to false, then visitor will have to wait whole time 
specified by set_timer method, if visitor will try to 
refresh page, timer will be reset and start over again
(class uses session to maintain that)
*******************************/
$popunder->set_exit(true);

/*******************************
There is an opton to mark visitors that already 
viewed advertisment, to not bother them too often, using cookies.
The "not bothering" time depends on time set using
set_period() method in seconds, 
by default it is 1 day, but you may change it if you wish.
If you choose not to use visitor marking 
(by default it is on, but you can disable it using method below)
then popunder ads will appear based on specified probability
on every pageview, which could be pretty annoying
if probability is high
*******************************/
$popunder->use_mark(false);

/*******************************
if visitor marking is true, then marked visitor
won't be bothered with pop under advertisments
for time period ins econds specified below
*******************************/
$popunder->set_period(60*60*24);


/*******************************
callback function is used to gather statistic 
about which advertisment was shown and was it
skipped by user, or fully viewed
Callback function needs to be declared before
passing function name to class
and it must accept 2 parameters
1. parameter - ID of advertisment you provided
		when adding advertisment to rotation
2. parameter - bool whether advertisment was skipped 
		by visitor. False means visitor didn't push
		skip advertising button/link
*******************************/
function call_back($id, $skip){
	//update database record etc, based on advertisment ID if you need to
	//outputting text is just to show you how it works
	echo "<p>Advertisment with ID ".$id." was shown and";
	if($skip)
	{
		echo " was skipped";
	}
	else
	{
		echo " wasn't skipped";
	}
	echo " by visitor</p>";
}
//function declared, now we can pass it to class
$popunder->set_callback("call_back");

/*******************************
add_url method ads avertisment to rotation
all advertisments appear in random order
and after user viewed advertisment 
callback function will be called.
Method add_url accepts 3 parameters:
1. - url of advertisment
2.(optional) - title or deswcribing text
3.(optional) - unique identifier which will be 
passed to callback function
*******************************/
$popunder->add_url("http://code-snippets.co.cc/", "Code snippets", 1);
$popunder->add_url("http://www.phpclasses.org/browse/author/838850.html", "Arturs Sosin's PHP classes", 2);

/*******************************
This is just for debugging purpose
if something isn't working as you expected
Shoouldn't be used in end version
*******************************/
if($errors = $popunder->get_errors())
{
	foreach($errors as $error)
	{
		echo "<p>".$error."</p>";
	}
}

/*******************************
And this method does all the magick
Use it before any other otuput to browser
*******************************/
$popunder->show_ads();


/*******************************
Some random content
*******************************/
echo "<p>Content</p>";
echo "<p>There is a 50% probability of popunder advertisment to appear, just keep clicking link</p>";
echo "<p><a href='?id=".($_GET['id']+1)."'>Goto ".($_GET['id']+1)."</a></p>";

/*******************************
OTHER OPTIONAL CUSTOMIZATIONS

Class language

You may want to change language used in user interface
Do that by editing language.php file, 
just keep aray structure and key names the same

Pop under template

You may customize popunder template in popunder_template.php 
to match your site design. Just leave layout and constant, 
you may change colors and sizes, etc
*******************************/


?>
Return current item: Pop under class