Get Logged Username

Permalink
Hey everyone,
I am wondering has the php code required to pull the logged in username changed with C5.7? I have tried the codes in the forum, the legacy documentation, have tried numerous codes off the Internet and can not get the user name. I've managed to grab admin, Array, a blank or not get any info at all. I am at a total loss here, any input would be highly appreciated.

Thanks!

jmfb
 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi jmfb,

You can try this:
// display information for the current signed in user
// 1. create a new User object
$u = new User();
// 2. get the UserInfo object based on the user's ID
$ui = UserInfo::getByID($u->getUserID());
// only display if a user is logged in
// - otherwise $ui is null and you get errors
if (is_object($ui)) {
    // display the user name
    echo $ui->getUserName();
}
jmfb replied on at Permalink Reply
jmfb
Thanks for your reply MrKDilkington. Unfortunately that code doesn't work for me. I'm not sure why....I thought it might have been 5.7 but it must be something to do with my site configuration?
Maybe the whole story will help shed light on what's wrong. Basically I have a game on my website. In the game I am doing an Ajax call to the php file. I am wanting that php file to get the logged in user name and send it to the game with Ajax. I know that the call works, I have tested that part....but for some reason the 'default' codes to get the logged in user from Concrete just aren't working for me.
MrKDilkington replied on at Permalink Reply
MrKDilkington
@jmfb

"In the game I am doing an Ajax call to the php file. I am wanting that php file to get the logged in user name and send it to the game with Ajax"

Is this PHP file external or in concrete5? Do you have a route registered in concrete5 that points to a class method?
jmfb replied on at Permalink Reply
jmfb
Everything is done within the Concrete5 website, the game, the php....everything. As for the route question, I would have to say...I don't know :)
jmfb replied on at Permalink Reply
jmfb
How would I go about doing that?

Thanks!
MrKDilkington replied on at Permalink Reply
MrKDilkington
@jmfb

I don't have personal experience with this, but here is an example from the Community Store add-on that you might find helpful.

Example: getCartSummary()
- create the route to a method
https://github.com/concrete5-community-store/community_store/blob/92...
- define your method
https://github.com/concrete5-community-store/community_store/blob/92...
- setup the AJAX to use that route
https://github.com/concrete5-community-store/community_store/blob/92...

When you create your method, you can try using the previous code for getting the username.
jmfb replied on at Permalink Reply
jmfb
Hopefully I can use something here. Thanks MrKDilkington!
jmfb replied on at Permalink Reply
jmfb
I can't believe how difficult it is just to get the logged in user name.....I can't get any code to work.
jmfb replied on at Permalink Reply
jmfb
I was also thinking it might be easier to have the player sign in with their C5 user name and password at the game level, then when the account has been confirmed by comparing to the C5 database, that user name can then be used to generate the unique save identifier I am after. If I was to do this and queried thae database would this work,or is there a modification need here?

SELECT * FROM `users` WHERE uName = 'admin' AND uPassword = md5(‘passwordhere’)

Thanks!
mnakalay replied on at Permalink Reply
mnakalay
where is that php file placed in your folder structure?
jmfb replied on at Permalink Reply
jmfb
The php file making the call? It is in the game folder.
jmfb replied on at Permalink Reply
jmfb
I've also tried an ajax call to the 'Login Dialog' plugin....got an Access Denied. I have no idea what else I can do to retrieve the logged in user name and I really need to figure this out.
mnakalay replied on at Permalink Reply
mnakalay
The fact that you are getting an "access denied" seems to show that either you are missing the first line
defined('C5_EXECUTE') or die(_("Access Denied."));


Or your file is outside of the C5 system and can't access its API.

You might have to show us some code to get more help.
jmfb replied on at Permalink Reply
jmfb
I get the Access Denied when attepting to ajax call the free plugin "Login Dialog". This was my last ditch effort after trying

<?php
$u = new User();
print $u->getUserID();
?>

I've tried this and several variations of this (including the example given by MrKDilkington above) to get the logged in user....nothing has worked for me at all.
mnakalay replied on at Permalink Reply
mnakalay
What I meant is you might have to show us the whole thing, preferably zipped with the proper folders so we can test locally