Author Topic: SMFForum - Show Ads to guests & members, though let members hide them  (Read 1816 times)

0 Members and 1 Guest are viewing this topic.

Offline admin

  • Administrator
  • *****
  • Posts: 3,006
  • Karma: +30/-0
    • View Profile
This is a simple trick I run on RMRK to get some extra help with ad revenue. Showing ads to guests is great. It works better providing a small notice along with the ads informing the guests that they won't see them if they login / register also.
You could just stick with that, but a forum such as RMRK is browsed almost entirely by logged in people.
I personally have a dislike of ads (except when they're mine of course=p ), so I don't want to force them on the users. A great way to allow them to hide them is a simple cookie system.
Go to the theme you want to add the ads to's directory and download the template file that you want to show ads for. You can usually work these out by the names. Display.template.ph p is used only when displaying topics, Profile.template.ph p is used when vieing profiles and so on. I've added this to my Index.template.php. This is the main template for your forum, and adding stuff in here should make it constant across all pages of your forum.
Here is my code, in snippets:
Code: [Select]
    //Ads - New approach.

    if(isset($_GET['hideads'])){

    setcookie("no_ads", "useless_value", time()+172800);  /* expire in 2 days */

    echo '<!-- COOKIE SET -->';}
This is the first part, and what this code will do is actually set the cookie if a request to hide ads has been sent in the URL. We'll give this option to hide later on. The cookie has a value of "useless_value" since we don't actually need any value in the cookie, we're just going to check whether or not it exists later on. the time is in seconds, and 172800 is 2 days. Increase or decrease this depending on how often you want the cookie to expire and the member to start seeing ads again.
     
Code: [Select]
  if ($context['user']['is_guest'])
{
echo '
<div align="center">
<table><tr><td class="titlebg2" width="100%" style="border-top: 0;">
<script type="text/javascript"><!--
google_ad_client = "pub-7302574677795924";
google_alternate_color = "EEEEEE";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
//2006-12-12: RMRK
google_ad_channel = "8337678587";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "7880A8";
google_color_text = "000000";
google_color_url = "7880A8";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<small>Registered/Logged in users can hide advertisements and also have access to additional forums and features. Please Log in or consider <a href="', $scripturl, '?action=register;" title="Register">registering</a>.</b></small>
</td></tr></table>

</div>
';

}
Here I check if a user is a guest, and if they are, just show an ad along with a message letting them know they can hide them if logged in. I also try and reel them in by letting them know that they can't see a couple of special forums whilst not registered =p
 
Code: [Select]
else {

if(isset($_COOKIE['no_ads'])){
echo '<!-- no ads -->';}
else{
echo '
<div align="center">

<table><tr><td class="titlebg2" width="100%" style="border-top: 0;">
<script type="text/javascript"><!--
google_ad_client = "pub-7302574677795924";
google_alternate_color = "EEEEEE";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
//2006-12-12: RMRK
google_ad_channel = "8337678587";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "7880A8";
google_color_text = "000000";
google_color_url = "7880A8";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br><a href="', $scripturl, '?hideads=blah;" title="Hide this ad?" onclick="return confirm(\'This will make these ads go away for a few days.\');">(Hide this?)</a>
</td></tr></table>
</div>
';

}

}
Ths next part uses an else statement. Since they're not a guest, we know they're logged in. We first check for the presence of a "no ads please" cookie, and if there is one, we don't show any ads (instead I just inset a HTML comment saying so). If the cookie isn't there, we show an ad, like we did to guests, though we also give them the cookie setting link that the first part of this code uses to set the cookie. When they click the hide link we show a confirmation dialog box just to make sure they want to hide them. =p
And that's it! A guest cannot work around this, as the cookie's presence is only checked for once we've determined they they aren't a guest. You could easily adapt this so that, say, only people in a certain membergroup or post group could have the ability to hide them; it's just a case of altering the variables that are checked against.



By : Silverline
« Last Edit: September 12, 2007, 02:00:25 AM by Admin »