Can not get current user id in php code

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
rudi_br
Registered User
Posts: 42
Joined: Sun Dec 10, 2017 5:48 pm
Name: Rudi Bjørn Rasmussen

Can not get current user id in php code

Post by rudi_br »

Hi,

I am trying to get the user_id for current logged in user in MyFile.php.

I have enabled php in ACP board security and added this to my template overall_footer:

Code: Select all

    <!-- PHP -->
    {
        include 'MyFile.php';
        MyFunction();
    }
    <!-- ENDPHP -->
So far so good, the function executes, and I can get it to output with echo, so I know it's executed.

However, when I try to get current user_id like this, then the output is just "User: " - because the last part getting user_id is null.

Code: Select all

function MyFunction()
{
        echo 'User: '.$user->data['user_id'];
}
I have tried this function as well, which works if executing MyFile.php directly, but not via overall_footer through phpBB.

Code: Select all

function MyFunction()
{
	define('IN_PHPBB', true);
        $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './forum/';
        $phpEx = substr(strrchr(__FILE__, '.'), 1);
        include($phpbb_root_path . 'common.' . $phpEx);    
        // Start session management
        $user->session_begin();
        $auth->acl($user->data);
        $user->setup();
        echo 'User: '.$user->data['user_id'];
}
What am I doing wrong?
/Rudi Bjørn Rasmussen
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 4259
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.
Contact:

Re: Can not get current user id in php code

Post by thecoalman »

You need to pass the variable(s) to the function or use global.

Code: Select all

function MyFunction($var1, $var2)
{
        echo $var1;
        echo $var2;
}

$a = 1;
$b = 2;

MyFunction($a, $b);
-OR-

Code: Select all

$a = 1;
$b = 2;

function MyFunction();
{
        global $a, $b;
        echo $a;
        echo $b;
}

MyFunction();
https://www.php.net/manual/en/language. ... .scope.php
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
rudi_br
Registered User
Posts: 42
Joined: Sun Dec 10, 2017 5:48 pm
Name: Rudi Bjørn Rasmussen

Re: Can not get current user id in php code

Post by rudi_br »

Thanks for the answer. I am aware of variables in the php language.

I can try to boil it down a little.
If I add the following block in 'overall_footer.html', it only outputs "User: " in the footer.

My problem is, that I cant seem to get the user_id from within overall_footer...

Code: Select all

	...
    <!-- code added by Rudi -->
    <!-- PHP -->
    {
        $user_id = request_var('u', $user->data['user_id']);
        echo 'User: '.$user_id;
    }
    <!-- ENDPHP -->
    <!-- codeblock ends -->
	...
/Rudi Bjørn Rasmussen
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 4259
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.
Contact:

Re: Can not get current user id in php code

Post by thecoalman »

My apologies, I don't use php in templates. I don't how or if you make variables available in them. Not sure what your needs are but you can create custom template variables. In Includes/functions.php around line 4458

Code: Select all

	// The following assigns all _common_ variables that may be used at any point in a template.
	$template->assign_vars(array(
		'SITENAME'						=> $config['sitename'],
		'SITE_DESCRIPTION'				=> $config['site_desc'],
Down a little further is the template variables for the footer itself. The user id may already be available as template variable but you could just add your own. Template variables specific to a page are also available at the bottom of each main script, viewforum.php etc.
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
rudi_br
Registered User
Posts: 42
Joined: Sun Dec 10, 2017 5:48 pm
Name: Rudi Bjørn Rasmussen

Re: Can not get current user id in php code

Post by rudi_br »

Thanks for the reply - your comment
I don't use php in templates
made me thinking. And I tried moving my include and function call to index.php - and now it works like a charm. I think the snag was that I injected php in template code...

But the main thing is that now it works as intended and I am happy ;-)

Thanks @thecoalman!
/Rudi Bjørn Rasmussen
Post Reply

Return to “phpBB Custom Coding”