7 Replies - 5066 Views - Last Post: 15 March 2012 - 05:16 AM Rate Topic: -----

#1 hathead1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-November 11

HOW TO ECHO USERNAME TO LOGGED IN USER!

Posted 14 March 2012 - 12:33 PM

Hi again all. Just wondering if anybody knows how to echo a logged in user's (username).

Tried the code below but this doesn't work. I have declared the session as ($_SESSION['username'] = true;) - this code is in my check-login before everything else. Please note that this works for stopping people accessing pages without logging in but wont let me do a simple thing like echo a logged in username. Thank you in advance.

Code that I have tried

<?php 
$username = $_SESSION['username']; 
?> 



Is This A Good Question/Topic? 0
  • +

Replies To: HOW TO ECHO USERNAME TO LOGGED IN USER!

#2 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5072
  • View blog
  • Posts: 26,471
  • Joined: 10-May 07

Re: HOW TO ECHO USERNAME TO LOGGED IN USER!

Posted 14 March 2012 - 12:40 PM

<?php
	if($_SESSION['username']) echo $username;
	else echo "Username not set";
?> 


Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

Re: HOW TO ECHO USERNAME TO LOGGED IN USER!

Posted 14 March 2012 - 12:45 PM

This may look longer and harder, but it's really shorter and easier than what you're probably doing:
<?php
class myuser {
	public $loggedin;
	pbulic $username;
	
	function __construct($name = "Guest", $status = 0) {
		$this->username = $name;
		$this->loggedin = $status;
	}
}

session_start();

if(isset($_POST['login'])) {
	$is_logged_in = check_login();// You can write this function!
	$A_user = new myuser($_POST['name'], $is_logged_in);
} elseif(isset($_SESSION['myuser'])) {
	$A_user = $_SESSION['myuser'];
} else {
	$A_user = new myuser();
}

echo $A_user->username."<br>";
	
$_SESSION['myuser'] = $A_user;
?>


Was This Post Helpful? 3
  • +
  • -

#4 hathead1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 25-November 11

Re: HOW TO ECHO USERNAME TO LOGGED IN USER!

Posted 14 March 2012 - 04:07 PM

View Postno2pencil, on 14 March 2012 - 07:40 PM, said:

<?php
	if($_SESSION['username']) echo $username;
	else echo "Username not set";
?> 





Hi tried the code you gave me. Keep getting error "Undefined variable: username" even thore I declared the session in the check login.
Was This Post Helpful? 0
  • +
  • -

#5 xxxjj18  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 53
  • View blog
  • Posts: 167
  • Joined: 30-November 11

Re: HOW TO ECHO USERNAME TO LOGGED IN USER!

Posted 14 March 2012 - 05:28 PM

To terminate the error for the username variable, perform a check to see whether the variable exists or not, and then implement your code;

session_start();

if(!empty($_SESSION["username"]) {
   print "Hello, {$_SESSION["username"]}";
}else{
  print "You're not logged in!!";
}



The reason you get the error is because PHP assumes that the username variable is already defined; when it is not.

This way, you can avoid any error messages :)
Was This Post Helpful? 0
  • +
  • -

#6 xxxjj18  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 53
  • View blog
  • Posts: 167
  • Joined: 30-November 11

Re: HOW TO ECHO USERNAME TO LOGGED IN USER!

Posted 14 March 2012 - 05:28 PM

To terminate the error for the username variable, perform a check to see whether the variable exists or not, and then implement your code;

session_start();

if(!empty($_SESSION["username"]) {
   print "Hello, {$_SESSION["username"]}";
}else{
  print "You're not logged in!!";
}



The reason you get the error is because PHP assumes that the username variable is already defined; when it is not.

This way, you can avoid any error messages :)
Was This Post Helpful? 0
  • +
  • -

#7 Macjohn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 407
  • Joined: 10-April 09

Re: HOW TO ECHO USERNAME TO LOGGED IN USER!

Posted 15 March 2012 - 01:35 AM

try setting a session variable for the username, like that you may use it in every page.

When I login I set that variable and then i use it to post the username on the top of the page.
I use it like this:

When I login:
if($result) {
		if(mysql_num_rows($result) == 1) {
			//Login Successful
			session_regenerate_id();
			$member = mysql_fetch_assoc($result);
			$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
			$_SESSION['SESS_MAIL_ID'] = $member['login'];
			$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
			$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
			session_write_close();
			header("location: user_index.php");
			exit();

And this when he is logged in:
Welcome <?php echo $_SESSION['SESS_FIRST_NAME'];?>

Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5072
  • View blog
  • Posts: 26,471
  • Joined: 10-May 07

Re: HOW TO ECHO USERNAME TO LOGGED IN USER!

Posted 15 March 2012 - 05:16 AM

View Posthathead1990, on 14 March 2012 - 07:07 PM, said:

View Postno2pencil, on 14 March 2012 - 07:40 PM, said:

<?php
	if($_SESSION['username']) echo $username;
	else echo "Username not set";
?> 





Hi tried the code you gave me. Keep getting error "Undefined variable: username" even thore I declared the session in the check login.

Oops, my bad! I was using a combination of both of your variables, :(

<?php
	if($_SESSION['username']) echo $_SESSION['username'];
	else echo "Username not set";
?> 



However, I would like to point out that the other examples given are much more thorough than mine. You should follow their examples over what I've done here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1