News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

php to return list of usergroups: can't seem to make it work

Started by spinhead, May 25, 2018, 01:05:43 PM

Previous topic - Next topic

spinhead

I'm far from expert with php but I'm not a noob. I think I'm missing something about SMF.

Using this code to troubleshoot a larger issue. I want to see what groups a user is in.

echo 'groups [';
print_r(array_values($userinfo['groups']));
echo ']';


What's on the page is

groups []

As in, it's blank.

All I want to do is verify whether a user is in group 1 or 9 (my custom subscribers group) and then NOT display a certain message. I have the if/then set up, but it always displays whether I'm logged in as an admin (group 1) or as a regular member (neither 1 nor 9.) Upon checking I discovered I'm not getting a group id no matter what I do.

Shambles

Where did you get $userinfo from?

You probably mean $user_info

spinhead

You are correct about my typo. Fixing that doesn't have any visible effect.

Trying this now

echo 'groups [' . $user_info['groups'] . ']';

echo 'array values [';
print_r(array_values($user_info['groups']));
echo ']';


and in both cases it returns nothing.

Shambles


spinhead

in /default/BoardIndex.template.php

When I say it's blank, I mean this is what displays on the page

groups []array []

My text is showing up as expected, but between the brackets there's nothing where I'd hope the group id would be.

Illori


Shambles

^ that would be my next question.

Here's the gist of what you'd put in there


<?php
global $user_info;

$test = array('1','9');

if(!empty(
array_intersect($user_info['groups'],$test)))
  echo 
'yes';
else
  echo 
'no';
?>

spinhead

Looks like I'll have to retract my statement about not being a PHP newbie.

Thank you very much for the solution.

Now I'll go read up on globalizing.

Shambles

Yeah it's a common mistake, so don't take it to heart.

A variable has to be "in scope" to be useful; if declared in an outer module you have to provide linkage to it, via the "global" directive.

Good luck with your endeavour :)

Advertisement: