4
#1
Example 9: Accessing User Information (all boards)
Posted June 24th 2011, 12:16pm
In the last couple years, I've seen a lot of requests on the forumotion help board for the ability to display certain content for guests, and different content for members. That is an easy thing to do, if you think outside the box and know a little javascript. Or you can read this tutorial to find out how to do it. :D

While this technique is simple, it is extremely effective, and it will work on all forumotion board types. I use this exact technique in the scripts on this board! For example, it is how the login and info boxes in the logo area are replaced with a member info box after a member has logged in.

So on to the technique! The first thing you must do is add the following information to your site description:

<span id=unl>{USERLINK}</span><script>ddginit();</script>

This will put the user information in a SPAN tag, and then a javascript function is called that takes the user information from the PHP variable and makes it accessible to other scripts.

You will now need to create a javascript file, in all the pages, containing the following:

var CopyrightNotice = 'User info code for forumotion boards. Copyright © 2011 by Dion Designs. All Rights Reserved. Use and/or modification of this script is allowed, provided this entire copyright notice remains in the original or modified script. Distribution is not allowed without written consent from Dion Designs.';

var uid='';
var uname='';

function ddginit() {
var x=document.getElementById('unl');
x.style.display='none';
uid=x.childNodes[0].href.substring(x.childNodes[0].href.indexOf('&u=')+3);
uname=x.childNodes[0].innerHTML;
x.innerHTML='<span id="uid">'+uid+'</span><span id="uname">'+uname+'</span>';
document.body.insertBefore(x,document.body.firstChild);
}

Once you create the file, when each page on your forum is loaded, you will have the user's name and member number available for whatever you want! And as an added bonus, all guests are given a member number of -1, which gives you the ability to know whether a member or a guest is viewing the page.

Here's an example of what you can do. Let's take the basic file and expand it for an IPB2/Invision board:

var CopyrightNotice = 'User info code for forumotion boards. Copyright © 2011 by Dion Designs. All Rights Reserved. Use and/or modification of this script is allowed, provided this entire copyright notice remains in the original or modified script. Distribution is not allowed without written consent from Dion Designs.';

var uid='';
var uname='';

function ddginit() {
var x=document.getElementById('unl');
x.style.display='none';
uid=x.childNodes[0].href.substring(x.childNodes[0].href.indexOf('&u=')+3);
uname=x.childNodes[0].innerHTML;
x.innerHTML='<span id="uid">'+uid+'</span><span id="uname">'+uname+'</span>';
document.body.insertBefore(x,document.body.firstChild);
}

$(function() {
if (uid!='-1') {
$('#submenu li').append('<a class="mainmenu" href="/u'+uid+'">Public Profile</a>');
}
});

What this code does is check whether the person viewing the page is a logged-in member, and if so, adds a link to the navbar. And not just any link -- a link to the member's public profile, a feature requested numerous times on several boards!

If you find a good use for this technique, I would appreciate your letting me know in a reply. Happy coding!
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Example 9: Accessing User Information (all boards)
Posted June 27th 2011, 10:48am
I always took information like this out of the log-out link. :P
And.... I never even thought about the user id. Another great idea from Dion! :D
φ
Posts: 64
Joined: June 17th 2011, 11:00pm
Location: 127.0.0.1/
Likes Given: 3
Likes Received: 2
1
#3
Example 9: Accessing User Information (all boards)
Posted June 27th 2011, 11:06am
Those with PunBB and phpBB2 boards should be particularly interested in this technique since the { USERLINK } variable is not available in templates.

The technique for templates is simple. First, add this to your site description:

<span id=unl>{USERLINK}</span>

Now find the site description in the overall_header template and add the script code afterwards, enclosing the code in a SCRIPT tag. Here is how it would be done in PunBB:

<!-- BEGIN switch_desc -->
<p id="pun-desc">{switch_desc.SITE_DESCRIPTION}</p>
<!-- END switch_desc -->
<script type="text/javascript">
var CopyrightNotice = 'User info code for forumotion boards. Copyright © 2011 by Dion Designs. All Rights Reserved. Use, modification, and/or distribution of this script is allowed, provided this entire copyright notice remains in the original, copied, or modified script.';

var x=document.getElementById('unl');
x.style.display='none';
var uid=x.childNodes[0].href.substring(x.childNodes[0].href.indexOf('&u=')+3);
var uname=x.childNodes[0].innerHTML;
x.innerHTML='<span id="uid">'+uid+'</span><span id="uname">'+uname+'</span>';
document.body.insertBefore(x,document.body.firstChild);
</script>
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Example 9: Accessing User Information (all boards)
Posted June 29th 2011, 11:28pm
I have updated the previous post to include a the procedure for adding this information in PunBB and phpBB2 templates.

If you have a question or comment, please reply to this topic. Happy coding!
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Example 9: Accessing User Information (all boards)
Posted July 10th 2011, 8:18am
Hi, i have added these scripts to my board via the forumotion javascript section in the module tab.
(and site description)

I was just wondering if i could your brains for a few uses of it since my javascript skills extend no further than copy and pasting someone else's script.
(i'm fine with html thankfully)

So i want to be able to access the User ID just like any other variable. So i can include the variable within codes and such like's.

But say for example i wanted to put on the homepage 'Hi there your user id is: USER ID VARIABLE HERE'
what would i put?

I know its a silly example, and i doubt i will be doing that but it will defnitely help me understand a bit better.

Thank You.
φ
Posts: 509
Joined: July 9th 2011, 11:00pm
Location: England
Likes Given: 15
Likes Received: 26
Example 9: Accessing User Information (all boards)
Posted July 10th 2011, 10:51am
If you added the user information code as you described, you can use the following code in your homepage message:

<script type="text/javascript">
var uid=document.getElementById('uid').innerHTML;
document.write('<div class="idwelcome">Hi there your user ID is: ' + uid + '</div>');
</script>

I added a class name of .idwelcome to allow CSS formatting of the line. I hope this helps!
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Example 9: Accessing User Information (all boards)
Posted July 10th 2011, 2:37pm
Hi again, thanks for your help.

So in order to get the user ID it needs to refference the script with 'getElementbyID('uid') rather than just typing '+uid+' somewhere.

So say i wanted to include the user ID in to a script which needs to change the user id depending on who the user is.

Basically i am setting up a 'Status Changer' for my forum. and i have set up a widget for the sidebar, which will basically be the same as the 'Status' box in the profile. So the 'Status' profile field can be changed using this box in the sidebar.
However, it works fine for me, and not everyone else, i realised its because the code require's the user ID, so when i put the user id as 1, it works for me, but obviously i need this to change for the other users, which is why i was looking for a USER ID variable.

So within my script if i include the above code without the 'hi there your user id is:' and just leave in the '+user+' bit in the div tags, it should insert the user id there into the code right?
φ
Posts: 509
Joined: July 9th 2011, 11:00pm
Location: England
Likes Given: 15
Likes Received: 26
Example 9: Accessing User Information (all boards)
Posted July 10th 2011, 4:05pm
@Jordan: What the user information code does, is add the username and ID into a hidden span on the top of the page. Accessing the DOM is not required if you put the Status Changer code in the same script ase the user information code.

If I have understood you correctly, you have a fixed sidebar on your forum, and that sidebar contains a form. In that form you have a status field, and a user id field. Your solution is simple, though. First, add type="hidden" to your field. Then, add id="identification".

The key in this will be your script. Below your sidebar, add this script:

<script type="text/javascript">
$(function() {
var uid = document.getElementById('uid');
document.getElementById('identification').value = uid;
});
</script>

Now your user id field is automatically filled in. :D
φ
Posts: 64
Joined: June 17th 2011, 11:00pm
Location: 127.0.0.1/
Likes Given: 3
Likes Received: 2
Example 9: Accessing User Information (all boards)
Posted July 27th 2011, 12:54pm
Using this technique, I modified the Online Stats pop-up on the homepage. Members will see the stats normally. Guests will not. :D
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Example 9: Accessing User Information (all boards)
Posted September 29th 2011, 2:15pm
Hahh. :)
With this script, I added admin BBcode buttons like [gal]something[/gal]... Only I can see that buttons. Anyone else can't. :)
φ
Posts: 37
Joined: August 27th 2011, 11:00pm
Location: Србија/Serbia
Likes Given: 4
Likes Received: 2
Topic locked
10 posts

Who is online

Users browsing this forum: No registered users and 1 guest