Log in

View Full Version : trying to get user name in widget


Disco_Stu
05-07-2012, 07:55 AM
I'm trying to retrieve the user name - or just see if the user is signed in - in an HTML widget using javascript but it isn't working

I tried using this but it didn't work

var username =$bbuserinfo[username];


Can someone post a sample script?

Thanks

LifesGreatestGift
05-07-2012, 08:36 AM
cant you just use a php widget and do this

if ($vbulletin->userinfo['userid'] > 0) {
$username = $vbulletin->userinfo['username'];
echo 'Welcome ' . $username . '!';
} else {
echo 'Welcome Guest!';
}


P.S. I doubt the code above works, its a proof of concept.

Try this if you want to stick to JS

var username =$vbulletin->bbuserinfo['username'];

not sure if single quotes are needed in JS.

Disco_Stu
05-07-2012, 10:52 AM
The code doesn't work in my java function and causes the function to stop due to invalid syntax

var username =$vbulletin->bbuserinfo['username'];

I'm trying to get the user name in a Static HTML widget using java.

kh99
05-07-2012, 11:09 AM
The problem is that a static html widget is not a template, so you can't use any variables, only html. You would have to use a php direct execution type widget and set $output to your html (although getting quotes and escape characters right can be tricky). You could also make your html a template then just use a php type widget that renders your template, like:


$template = vB_Template::create('my_widget_template');
$output = $template->render();
unset($template);



Then in your template you can use

var name = '{vb:escapeJS {vb:raw bbuserinfo.username}}';

Disco_Stu
05-07-2012, 11:13 AM
Are there any functions or API I can call from java that will return the current user name or tell me if the user is logged in? I don't want to rewrite the widget as it has a lot of java code in it.

I tried using the userid cookie but for some reason it is not always present even when I log in.

kh99
05-07-2012, 11:19 AM
Javascript runs in the browser, so it only has access to data that's included in the html document. If you look at the headinclude template there's a section that sets some javascript variables just for that purpose (so the data is available in js functions on the page). So you could edit the headinclude template and add the username there.

If you switched to a php widget with a template I don't think you'd have to rewrite the widget, you'd just copy what you have to a template and the widget code would be a few lines as posted above (if you wanted you could try it by creating a new widget so if it doesn't work out you can just delete it).

Disco_Stu
05-07-2012, 11:22 AM
I'll try the head include approach and see if that works. Do I simply assign the variable value there?

Also, any idea why I don't always see the bb_userid cookie even when logged in? At what point is it created?

kh99
05-07-2012, 11:24 AM
I don't know about that cookie. I almost suggested that in my previous post but I seemed to remember that it wasn't always set.

Disco_Stu
05-07-2012, 11:27 AM
Could you give me an example of the syntax for the header include? I set a variable to the userid but my widget doesn't seem to recognize the variable so I must be doing something wrong

kh99
05-07-2012, 11:30 AM
In the template headinclude, there's this code:

var SESSIONURL = "{vb:raw session.sessionurl_js}";
var SECURITYTOKEN = "{vb:raw bbuserinfo.securitytoken}";
var IMGDIR_MISC = "{vb:stylevar imgdir_misc}";



So I was thinking of adding a line like:

var USER_NAME = "{vb:raw bbuserinfo.username}";


To be honest I'm not sure what special chars in a user name might cause problems with this, but in any case it seems like that should allow you to use USER_NAME in your js code.

Disco_Stu
05-07-2012, 11:33 AM
That one worked. It's just what I needed.

Thanks for all the help.