UBB.Dev
Posted By: Troberg Stupid newbie question: How to get user name? - 08/11/2005 10:52 AM
First of all, though an experienced programmer (VB, C, C#, REXX and a bunch of other languages), I'm a total PHP virgin and fairly new to UBB Threads, so please excuse me if I'm missing something obvious here.

I'm trying to tie some stuff together (for the moment it's my own dynamically threaded chat, but more will follow) and need to get the name of the logged in user. After digging around in the UBB source and these forums, I found that it seems to be stored in $user['U_Username'] and $CheckUser.

So far so good. The problem is that when I access them, they come up empty, which leads me to believe that I should include something where these are defined. Three hours of digging through the source and I still can't find it.

I've narrowed it down in this simple script:

Code
 <br /><html><br />  <head><br />    <title>Test</title><br />  </head><br />  <body><br />    <p><br />      <?<br />        echo 'Test string';<br />        echo $user['U_Username'];<br />        echo $CheckUser;<br />      ?><br />    </p><br />  </body><br /></html> 


This script results in "Test string" and nothing else. As I said, I'm probably just missing something obvious because I'm staring in the wrong place, but this silly problem has me stuck.

What am I doing wrong and how should I fix it?
First you have to require the library:

Code
 // Require the library<br />require ("./includes/main.inc.php"); 


Then you have to use the authenticate user function:

Code
 $userob = new user;<br />$user = $userob -> authenticate(); 


Then you could do something like this:

Code
 isset($user['U_Username']) && $Username = $user['U_Username']; 


and that would give you the user name

Note: The authenticate function will give you: U_Username,U_Password,U_SessionId, U_StyleSheet, U_Status, U_Privates, U_FrontPage, U_Number, U_Banned,U_LastOn, and U_OC_Userid plus it can accept input from you on a lot of other user data (just take a look at the showflat.php file for an example of what you can pull out of the database).
Thanks, worked like a charm. Would have taken me a while to figure out. PHP is syntactically a bit different from what I'm used to, and up til now I've just modified existing PHP code. It needs a decent editor also to color code it, otherwise it quickly starts to look like ASCII art when mixed up with HTML...

Once again, thank you.
PHP is kinda like "coding in shorthand" and a lot of the commands are condensed -it confused me at first and I had some experience in perl!

Glad I could help.
What makes PHP messy in my head is not the "shorthand-effect". It's that my head has trouble switching back and forth between HTML and PHP. I'm going to try to get my editor to use a different colored background for PHP and HTML, that should make some of the "special character alphabet soup" a bit clearer.

My chat works fine now, feel free to check it out at http://rpglab.net/index.php. It's in Swedish, but if you click on "chat", click on the link that appears, get the file, then click chat again it should work nicely even if you don't speak Swedish.

Edit: No need to register a user, just set your name in chat options.

It is IE only for the moment, but if you don't use IE, the file you downloaded also contains a windows stand-alone client.

As far as I know, it is the only threaded chat today. The oldest entries will be removed (200 kept, can be changed by the user), causing the threads to dynamically grow and adapt to the discussion. Difficult to explain, but works really nicely.
I use CuteHTML and it color codes the script pretty well.
© UBB.Developers