Posts RSS Comments RSS 5 Posts and 3 Comments till now

Archive for the 'SMF' Category

SMF - Show Ads to guests & members, though let members hide them

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.php is used only when displaying topics, Profile.template.php 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:

//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.

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

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.

This is very simple and of course not foolproof, and there will always be people who simply block all ads via extensions, but this works for the majority.

(Very) Simple SMF Level System

I can't stress the "simple" part enough for this one. This runs entirely on the template side. The level is calculated on the fly, and not stored in any variable or in the database where you can call it elsewhere. This is probably both an advantage and a disadvantage, depending on what you're planning to do with it.

I use this for RMRK, an RPG Making forum.

It calculates a level based off the user's postcount and member ID (lower (i.e. an older member) being better).

To use this is very simple, open up your theme's Display.template.php and find the following:

// Show how many posts they have made.

Either remove or comment out the default, which will almost always be something just like this:

echo '', $message['member']['posts'] ,' <br />'

and insert this instead:

$number = explode('.',round(pow (log10 ($message['member']['real_posts'] + ($context['common_stats']['latest_member']['id'] - $message['member']['id'])), 3),2));

$string = $number[0]. (isset($number[1]) ? ' ('. $number[1] . (strlen($number[1]) <2 ? '0' : '') .'%)' : '');

echo ' <br /><abbr title="Member ',$message['member']['id'],'  •  ',$message['member']['posts'],' Posts">Level ', $string , '</abbr><br />';

Here's an explanation of what is going on here:

$number = explode('.',round(pow (log10 ($message['member']['real_posts'] + ($context['common_stats']['latest_member']['id'] - $message['member']['id'])), 3),2));

This works out as follows: It starts off with the member's postcount. From here, we add the latest member's ID (a way of basically saying how many registered users there are on your forum), and then subtract from this the member's user ID.

If two members have the same amount of posts, the one with the lower user ID will have a higher number at the outcome of this equasion, resulting in a higher level. After all, they've been there longer! Then we do some extra math to generate the actual level from this number. It's such that as a level increases, it becomes harder to attain the next one. Finally the result is prepared to be interpreted by the next line..

$string = $number[0]. (isset($number[1]) ? ' ('. $number[1] . (strlen($number[1]) <2 ? '0' : '') .'%)' : '');

This takes the level number and formats it with a percentage on the end (if the level isn't an exact number). This gives a nice way of showing how far the member is towards the next level. Examples of how this would show:

Level 12

Level 5 (06%)

Level 40 (74%)

Now we have all this, let's display the member's level:

echo ' <br /><abbr title="Member ',$message['member']['id'],'  •  ',$message['member']['posts'],' Posts">Level ', $string , '</abbr><br />';

Since the level is generated from Postcount and User ID, I provide this information in a tooltip if a viewer hovers over the level. I also do this since on my own forum, user IDs and postcount aren't shown on the message display. You could do this differently for your own forum. I've only tested and used this whilst inside the display template, though it should also be usable on a member's profile

Simple Integration of PJIRC with SMF

I wrote this simple integration just for my own forum, though after a few requests from some friends running their own SMF forums, I'm going to outline just how I did this.

The absolute first thing you need to do is to get and upload some Java files. These are PJIRC, a popular Java IRC Applet. Get them here: SMF IRC Java Files. Upload these files to your forum's base directory.

The chat feature is accesed through the forum, as in via yourforum/index.php?action=chat. We're going to setup this custom "action" ourselves. Don't worry, this is very easy.

Navigate to your forum's base directory, and open up the index.php file. We need to add a new array for when "chat" is requested. Scroll down until you see the following snippet in the file:

// Here's the monstrous $_REQUEST['action'] array - $_REQUEST['action'] => array($file, $function).
$actionArray = array(

You'll see a long list of all the current valid requests. All we need to do here is add in a new line to handle a "chat" request, so add this: 'chat' => array('Chat.php', 'Chat'), .

Here's how mine looks after adding this:

// Here's the monstrous $_REQUEST['action'] array - $_REQUEST['action'] => array($file, $function).
$actionArray = array(
'activate' => array('Register.php', 'Activate'),
'chat' => array('Chat.php', 'Chat'),

As you can see, I've added "chat" after the activate one. That's all we need to do in index.php, so save this file and re-upload it to your forum's base directory. From here-on we'll be creating new files.

The first file we need to create will be called Chat.php. Note the capitalisation! In this file we only need a few lines. Add this to your file:

<?php
if (!defined('SMF'))
die('Hacking attempt...');

function chat() {
global $context;
$context['page_title'] = '#Crankeye @ irc.rmrk.net - RMRK\'s IRC Channel';
loadTemplate('Chat');
}
?>

As you can see, there are some things you'll want to edit for your own SMF forum. Namely, the page title variable. Remember to escape single quotes if you use any, like I have.

Once you've saved this file, upload it into the /Sources directory of your SMF directory.

The final file we need to create is a Chat.template.php file. This is the main page that your users will see and it's where we embed the Java files.

<?php
function template_main()
{
global $context, $settings, $options, $txt, $scripturl;

//Some stuff to fix up the username first!

$badchar = array(" ");
$newchar = array("_");
$chatusername = str_replace($badchar, $newchar, $context['user']['name']);

echo '
<style type="text/css">
.chat_header
{
font-family: \'trebuchet ms\', sans-serif;
font-size: 30px;
font-weight: bold;
letter-spacing: -2px;
line-height: 1em;
margin-bottom: 100px;
margin-top: 10px;
}
</style>

<table width="100%" border="0" cellspacing="0" cellpadding="3" >
<tr>
<td>', theme_linktree(), '</td>
</tr>
</table>

<table width="100%" border="0" cellspacing="0" cellpadding="4" align="center" class="tborder">
<tr class="titlebg">
<td align="center" ><span class="chat_header">RMRK Chat - #Crankeye</span></td>
</tr><tr>
<td class="windowbg">

<center><table><tr><td class="windowbg2">
<applet name="applet" code=IRCApplet.class archive="irc.jar,pixx.jar" width=640 height=400>
<param name="CABINETS" value="irc.cab,securedirc.cab,pixx.cab">

<param name="nick" value="',$chatusername,'">
<param name="alternatenick" value="Guest???">
<param name="name" value="Japplet">
<param name="host" value="irc.acidchat.net">
<param name="gui" value="pixx">
<param name="command1" value="/join #Crankeye">
<param name="quitmessage" value="Quit message!">
<param name="style:highlightlinks" value="true">

</applet>

</td></tr></table><br /><br /></center>

</td></tr></table>
</td>
</tr>
</table><br /><br />
';
}

?>

Quite a mouthful! Skimming through the above you should notice various bits that are specific and should be changed. I'll take you on a tour:

$badchar = array(" ");
$newchar = array("_");
$chatusername = str_replace($badchar, $newchar, $context['user']['name']);

This is just a really quick basic username fixing snippet, and works on the assumption that the username of almost anybody on your forum is likely to be made up of letters, numbers and spaces. You can't have spaces in an IRC nick, so this will replace spaces with an underscore, which is fine. Without this, a member named King Anesis would enter chat as King, though with this, he would enter as King_Anesis.

Skip ahead until you come to:

<span class="chat_header">RMRK Chat - #Crankeye</span>

You'll of course want to change this to suit your own forum. This is the header that will appear for the page, before the applet. Remember that you are writing inside a PHP echo statement, so if you use a single quote (') anywhere in your text, you must escape it (\') instead.

The applet embedding code needs no editing. Since you uploaded the Java files to your forum's base directory, this page will find them and load them (along with the language files) fine.

Skip ahead some more and we come to the params:

<param name="nick" value="',$chatusername,'">
<param name="alternatenick" value="Guest???">
<param name="name" value="Japplet">
<param name="host" value="irc.acidchat.net">
<param name="gui" value="pixx">
<param name="command1" value="/join #Crankeye">
<param name="quitmessage" value="Quit message!">
<param name="style:highlightlinks" value="true">

These should be obvious enough. The "alternatenick" value can have whatever you like in it, though it's a good idea to include at least 2 question marks in it. These question marks will be replaced by PJIRC with random numbers, ensuring that if there's a guest currently on, new guests will still be able to join. Change the "host" value to point to the IRC network that your own channel is on.

The "command1" value is a command that the client will automatically send upon connecting. Since you want the person to automatically be in your channel, we set this to "/join #yourchannel".

This is pretty much it! You can add whatever extra HTML underneath, around or above the applet if you wanted to.

One neat thing you can do is add buttons onto the chat page that will send data to the applet. Here's an example:

<FORM>
<INPUT TYPE=BUTTON VALUE="Smile" onClick="document.applet.setFieldText(document.applet.getFieldText()+\'=)\');document.applet.requestSourceFocus()">
<INPUT TYPE=BUTTON VALUE="How awesome is RMRK?" onClick="document.applet.sendString(\'RMRK ROX\')">
</FORM>

The first one simply puts text into the text field of the applet. The second one sends the text to the channel automatically, without the user having to send it.