Jump to content

Displaying Username of Logged In User


Unholy Prayer

Recommended Posts

Ok, I made my register, login, and logout scripts, but the last thing i need to know is once a user is logged in, how do I display their username?  For example, if Bob logs in under the username Bob, how do I make it say "Welcome, Bob."  But then it should change for every user.  If the user Henry logs in from a different computer, it should display "Welcome, Henry", not "Welcome, Bob".  Can someone help me with this?
Link to comment
https://forums.phpfreaks.com/topic/16301-displaying-username-of-logged-in-user/
Share on other sites

[quote author=Unholy Prayer link=topic=102687.msg407927#msg407927 date=1154508069]
Will the session scripting find the username of the person that is logged in?  If so, all I have to do is use <?php echo "Welcome, $username"; ?> ?
[/quote]

Or make sure you set the variable so it can be found, I'm not to crazy on php but did you try anything like,

[code]
<?php
$username=$_REQUEST["username"];
echo "Welcome, $username";
?>
[/code]

??
When you query the database in your login script, just pull out the username and set it to a session variable (assuming you are using sessions).

Your code should look something like this:

[code]<?php

session_start();

$username = $_POST['username'];
$password = $_POST['password'];

$query = mysql_query("SELECT * FROM table WHERE username='$username' AND password='$password' LIMIT 1");
if (mysql_num_rows($query) == 0){
    echo "<strong>Invalid username/password</strong>";
    exit();
}

$assoc = mysel_fetch_assoc[$query];
$_SESSION['username'] = $assoc['username'];

echo "<strong>Welcome, {$_SESSION['username']}!</strong>";

?>[/code]

Hope this helps
I tried the code you gave me, but I got an error on the following line:

[code]$assoc = mysel_fetch_assoc[$query];[/code]

The error was:
[quote]Parse error: syntax error, unexpected '[' in /home/mutantde/public_html/bbultimate/functions.php on line 16[/quote]
[quote author=Unholy Prayer link=topic=102687.msg407927#msg407927 date=1154508069]
Will the session scripting find the username of the person that is logged in?  If so, all I have to do is use <?php echo "Welcome, $username"; ?> ?
[/quote]

the seesion is stored on the users computer not the web server..so unless there both on a wierd screwed up busness network it will find correctly...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.