Jump to content

Archived

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

Unholy Prayer

Displaying Username of Logged In User

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?

Share this post


Link to post
Share on other sites
If there name is represented by a variable, why not echo the variable.. ?

Share this post


Link to post
Share on other sites
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"; ?> ?

Share this post


Link to post
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]

??

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
the line

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

should be

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

Share this post


Link to post
Share on other sites
You'll want to use parenthesesis - () and not sqaure brackets []:
$assoc = mysql_fetch_assoc($query);

Share this post


Link to post
Share on other sites
Whoops, simple typo.  Sorry about that.

Share this post


Link to post
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]

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...

Share this post


Link to post
Share on other sites

×

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.