kburnett
|
| Posted: 05/16/2002, 10:27 AM |
|
I see there has been quite a few posts about how to obtain/display a logged in user's name. Here's what I have done.
1) Created a "header" page and set it as the "Header Page" under Properties->Pages.
2) On the header page, I created a blank field and set the type to "label".
3) The I reworked the code I have seen everyone else use and rolled it into a fuction I add to Modules->Global Functions. I do this so that I can use this anywhere else in my application/web site.
function get_user_name()
{
// is there already a UserName in the session? If so, there is no need to query the db again.
if (get_session($UserName) == "")
{
// if the user is not logged in simply name the "Guest", else look up their user_name
if (get_session("UserID") == "")
{
set_session("UserName",Guest);
}else{
set_session("UserName",get_db_value("SELECT user_name FROM user WHERE id = " . get_session("UserID")));
}
}
return get_session("UserName");
}
**Note: I structured this code so that if a user is not logged in then I set the UserName to "Guest". If a user is logged in, I look up their user_name and add it to their session so that I don't have to look it up in the db every time I wish to user it (which in my case is on every page). There is a hole in this code: once you assign "Guest" to the session technically the code "if (get_session($UserName) == "")" is false and therefor a new UserName will never be assigned. But you don't have to sweat it because the session gets wiped clean on login and then the code flow as planned.
3) In the header->Form Properties->Events->Before Show I add $fldField1=get_user_name();
That is all there is to it.
Did I miss anything?
Kyle
|
|
|
 |
Nicole
|
| Posted: 05/17/2002, 2:01 AM |
|
Hello,
Since the function is called on header page it calls every time the page is loaded. To catch the moment when user loges in use:
function get_user_name()
{
if ((get_session($UserName) == "")&& (get_session("UserID") == ""))
{
set_session("UserName",Guest);
}
if (get_session(“UserID”))
{
set_session("UserName",get_db_value("SELECT user_name FROM user WHERE id = " . get_session("UserID")));
}
return get_session("UserName");
}
|
|
|
 |
schaeff
|
| Posted: 05/18/2002, 7:36 AM |
|
Then why not set the UserLogin in CCLoginUser?
My main problem was to access the field from a events script. In CCSb4 this seems to be working now.
|
|
|
 |
|