Get currently logged in TYPO3 User (fe-user)
Asked Answered
H

9

8

I'm usually not someone who posts in a forum, since I was always able to find something without bothering anyone. Anyway, this time I had no luck.

I have programmed a DB-Communications-System for my website, where you can leave messages for the rest of the family from a Java application, an Android app and the website itself. On this website you login through a fe_user login mask and then you can access the form. Here I have a dropdown selection 'From' and a dropdown selection 'For' where there are the names of the family to choose. What I want is for the page to read the currently logged in user and automatically set the 'From' Variable according to this user.

On many pages I have found

$GLOBALS['TSFE']->fe_user->user 

and the accordig variations of it, but no matter how I tried to get something out of it,

strleng() is always 0,
print_r($_GLOBALS['TSFE']) is always empty
and the whole Array is also empty.
What am I doing wrong? Do I have to do something before I can access these variables? Also in some cases, it doesn´t recognize this object and instead of interpreting the variable, it just says

"->fe_user->user"

on the website.

Thank you very much

Hafer answered 5/4, 2012 at 11:45 Comment(3)
do you write an Extension? You try to access via TypoScript? some more information would be helpful. t3lib_div::debug($GLOBALS['TSFE']->fe_user, 'the user object, if there is any');Cuspidor
How do you create your form? How do you include your PHP? I Guess there is no need to write PHP Code to archive what you want to do. Use TYPO3 instead:)Cuspidor
The form is a HTML Form that was supposed to POST the variables to the PHPHafer
J
22

print_r($_GLOBALS['TSFE']) is always empty

you should try $GLOBALS['TSFE'] and not $_GLOBALS['TSFE']

so $GLOBALS['TSFE']->fe_user->user is absolutely correct to check for a logged in user


Addendum 2019-08-15: Depending on what your are trying to achieve or what exact information you are looking for and what version of TYPO3 your are using there are different ways to use this variable.

For example to ensure the current user is authenticated with your TYPO3 you can retrieve his ID by

$GLOBALS['TSFE']->fe_user->user['uid'];
Jordon answered 23/10, 2012 at 9:41 Comment(3)
Be careful with that. $GLOBALS['TSFE']->fe_user->user can also store session data from not logged in FE users. As Mihir Bhatt suggests use $GLOBALS['TSFE']->fe_user->user['uid']; to be 100% sure.Mosley
Yes Right. $GLOBALS['TSFE']->fe_user->user is not working properly in TYPO3 8Therapy
The question and this answer is not about how but where to check for the current user, and especially doing this with the mentioned variable. How you proceed with this variable depends on what exactly your are trying to achieve.Jordon
R
8

If you need to get logged in user from TypoScript (and perhaps replace marker) this snippet of TS can be useful:

page.10.marks.UNAME = TEXT
page.10.marks.UNAME.data = TSFE:fe_user|user|username
Rohn answered 30/7, 2013 at 9:10 Comment(0)
V
5

This will return id of current logged in user

$GLOBALS['TSFE']->fe_user->user['uid'];
Vat answered 18/1, 2013 at 4:55 Comment(0)
A
3

TYPO3 11 and later

/** @var \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication $frontendUserAuthentication */
$frontendUserAuthentication = $this->request->getAttribute('frontend.user');

if ($frontendUserAuthentication->user) {
    $frontendUser = $this->userRepository->findByUid($frontendUserAuthentication->user['uid']);
}

Note:

Abrams answered 4/6 at 13:15 Comment(1)
What's the namespace of $this->userRepository?Clarissaclarisse
C
1

Install extension kickstarter and create an frontend-plugin for your own. You will get a simple form example. There you can use $GLOBALS['TSFE'] etc. (Check "By default plugins are generated as cachable USER cObjects. Check this checkbox to generate an uncached USER_INT cObject." while creating your frontend plugin!)

It is the old-school way to start extensions, but i guess in your case the quickest and easiest way to solv your problem.

Cuspidor answered 5/4, 2012 at 13:39 Comment(5)
If you use TYPO3, you should use the API. I added another possibility.Cuspidor
install the extension, kickstart new extension via extension manager. Start with your PHP code thereCuspidor
Do not stop reading. Only three items down there is "Frontend Plugins" which i meant with "create an frontend-plugin for your own".Cuspidor
This will kickstart an extension. Just create, install and look into typo3conf/ext/###your extension key name here###/Cuspidor
I kickstarted it, said its category 'frontend plugin' and under "Frontend Plugins" I said "Cache by default..." and "Add as a totally new Content Element type" and also tried "Add as a new Header Type" but still dont know what to do. Again, this cant be so complicated to find out the currently logged in user to give to a PHP Script?! I cant Imagine there wouldnt be a way to do so without having to write a new extension?!Hafer
P
1

@Halest and there's the sql injection vulnerability

    $ergebnis = mysql_query("select ses_userid from fe_sessions WHERE ses_id='$SessionID'");

By the way, never trust cookies. They can be modified or even turned off. Try to use more $GLOBALS['DB'] to interact with the database.

Proleg answered 29/8, 2014 at 8:35 Comment(0)
M
1
$GLOBALS['TSFE']->fe_user->user

This will return all data of fe_user table. you can get whatever value you want.

Memnon answered 7/4, 2015 at 13:5 Comment(0)
J
1

if you need the logged user in t3:

$user=null;

if($GLOBALS['TSFE']->loginUser){
 $user=$GLOBALS['TSFE']->fe_user->user;
}
Jesselyn answered 16/4, 2019 at 16:22 Comment(0)
C
0

You can use the extension powermail to create your form and to save the data to an database table (afaik, the table needs to has at least fields uid and pid).

Cuspidor answered 10/4, 2012 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.