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

??
Link to comment
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
Link to comment
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...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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