Access MyBB PHP variable with Javascript

1.3k views Asked by At

Is it possible to grab a MyBB PHP variable with Javascript? I am an userscript coder, and right now I use:

var uid = $("#panel").find('a').attr('href').replace(/[^0-9]/g, '');

To grab the user id (uid) of the current user. However, if I were to grab the UID from the user with PHP, it would look like this:

<?php echo {$mybb->user['uid']} ?>

Now to the actual question. Is it possible to grab the UID through Javascript, using the $mybb->user['uid']?

4

There are 4 answers

2
mituw16 On

First, you will not be able to directly access a php server side variable in javascript, but there are several work arounds you could do.

The first one that comes to mind is to put this in your html page somewhere

<script type="text/javascript">
    <?php echo "var uid = '". $mybb->user['uid']."';" ?>;
</script>

The downside to this is that this variable is now directly view able and editable in the DOM.

1
Toan Nguyen On

My answer is Yes, you can but it has some limits.

Well, if you remember in every headerinclude templates always have these code:

<script type="text/javascript">
<!--
    var cookieDomain = "{$mybb->settings['cookiedomain']}";
    var cookiePath = "{$mybb->settings['cookiepath']}";
    var cookiePrefix = "{$mybb->settings['cookieprefix']}";
    var deleteevent_confirm = "{$lang->deleteevent_confirm}";
    var removeattach_confirm = "{$lang->removeattach_confirm}";
    var loading_text = '{$lang->ajax_loading}';
    var saving_changes = '{$lang->saving_changes}';
    var use_xmlhttprequest = "{$mybb->settings['use_xmlhttprequest']}";
    var my_post_key = "{$mybb->post_code}";
    var imagepath = "{$theme['imgdir']}";
// -->
</script>

You can use MyBB's variables in any template with one condition: it's must be defined in the correlative PHP file. (Of course that variable must be inside the brackets { })

For example, the correlative PHP file with headerinclude template is global.php.

That's it. Have fun with MyBB :)

0
Jordan On

If you are using (or can use) a template you may place a PHP variable in a JavaScript variable defined within that template's content. If you plan to run this JavaScript on every page you must add the variable definition to the script block in the headerinclude template.

<script type="text/javascript">
<!--
    var userID = {$mybb->user['uid']};
// -->
</script>

That example script will place the user's uid in the JavaScript variable userID.

Please note that <?php echo $mybb->user['uid']; ?> will not work as MyBB's template system does not allow PHP tags within it. You may instead use {$mybb->user['uid'} as an alternative.

Also, take note that if you are using a plugin and eval'ing the template in a function in your plugin file you will also need to globalise the PHP variable you are going to use.

0
leefish On

You can add a plugin to your forum. It is called template conditionals. Once you have that installed you can use the Mybb->user variable in all templates. There are many applications of this plugin, but for your code you can do:

<setvar uservar>$mybb->user['uid']</setvar>

var {$tplvars['uservar']} = $("#panel").find('a').attr('href').replace(/[^0-9]/g, '');

The plugin is on the MyBBHacks forum at the link below.

http://mybbhacks.zingaburga.com/showthread.php?tid=464