Jump to content


Photo

Displaying Username of Logged In User


  • Please log in to reply
11 replies to this topic

#1 Unholy Prayer

Unholy Prayer
  • Members
  • PipPipPip
  • Advanced Member
  • 166 posts

Posted 02 August 2006 - 08:38 AM

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?

#2 sw0o0sh

sw0o0sh
  • Members
  • PipPipPip
  • Advanced Member
  • 151 posts
  • LocationMassachusetts

Posted 02 August 2006 - 08:39 AM

If there name is represented by a variable, why not echo the variable.. ?

#3 mastermike707

mastermike707
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 02 August 2006 - 08:39 AM

Just echo whatever variable contains the user's username where u want their name.
[url=http://www.mastermike707.com/new/][/url]

#4 Unholy Prayer

Unholy Prayer
  • Members
  • PipPipPip
  • Advanced Member
  • 166 posts

Posted 02 August 2006 - 08:41 AM

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"; ?> ?

#5 sw0o0sh

sw0o0sh
  • Members
  • PipPipPip
  • Advanced Member
  • 151 posts
  • LocationMassachusetts

Posted 02 August 2006 - 08:43 AM

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"; ?> ?


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,

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

??

#6 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 02 August 2006 - 08:53 AM

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:

<?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>";

?>

Hope this helps
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#7 Unholy Prayer

Unholy Prayer
  • Members
  • PipPipPip
  • Advanced Member
  • 166 posts

Posted 02 August 2006 - 09:50 AM

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

$assoc = mysel_fetch_assoc[$query];

The error was:

Parse error: syntax error, unexpected '[' in /home/mutantde/public_html/bbultimate/functions.php on line 16



#8 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 02 August 2006 - 10:21 AM

the line

$assoc = mysel_fetch_assoc[$query];

should be

$assoc = mysql_fetch_assoc[$query];


#9 Unholy Prayer

Unholy Prayer
  • Members
  • PipPipPip
  • Advanced Member
  • 166 posts

Posted 02 August 2006 - 10:29 AM

Hmm... I changed it, but I still get the same error on the same line.

#10 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 02 August 2006 - 10:54 AM

You'll want to use parenthesesis - () and not sqaure brackets []:
$assoc = mysql_fetch_assoc($query);

#11 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 02 August 2006 - 08:28 PM

Whoops, simple typo.  Sorry about that.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#12 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 02 August 2006 - 10:19 PM

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"; ?> ?


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...
"Syntax error" .. WHERE THE **** IS IT?!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users