Display users name when logged in

Hi all,

I am wanting to display the name of the user that has logged in.

I expect that I will need to use the session to do this.

Currently my session uses the following

 $_SESSION['user_id'] = $user['id'];
 $_SESSION['logged_in'] = time();

I am not sure how to use the sessions user name id to get the name of the user from the database.

Any help is appreciated.

Thanks

Adam

Try this:

  1. Create getUserName() function
    a. Open database
    b. Search table for “user_id” session variable if it exists
    c. Return “user_name” or “not logged in???”
  2. echo getUserName();

I don’t know how you have your application structured, so I don’t know where to suggest you put your code, but I see from a previous post that you are using (or looking into using) PDO. I also don’t know how your database table is set up, so this is just an example.

You need to write a query to the database something like this:

"SELECT first_name, last_name FROM users WHERE id = " . $_SESSION['user_id']; 

Then you can assign your user’s name to a variable and echo it.

1 Like

Thanks guys,

I went for this


<?php 


	require_once'connect.php';

$sql= "SELECT username FROM users WHERE id = " . $_SESSION['user_id'];
$stmt = $pdo->query($sql); 
$row = $stmt->fetch(PDO::FETCH_ASSOC);
}


 ?>

I have put this into a functions file, function.php

<?php 

function loggedInUsername($row) {
	require_once'connect.php';

$sql= "SELECT username FROM users WHERE id = " . $_SESSION['user_id'];
$stmt = $pdo->query($sql); 
$row = $stmt->fetch(PDO::FETCH_ASSOC);
}
 ?>

I am wanting to call it on another page.

I have the following

<?php 
require'function.php';
loggedInUsername()
echo $row['username']; ?>

This is not working and I feel it should be very simple but I can’t put my finger on where I am going wrong.

Thanks in advance

The function loggedInUsername was defined as taking a parameter $row. But when you called the function in your last bit of code, you didn’t pass this parameter through. Also, you didn’t return a value in that function (which should have been $row), so there would be nothing to echo.

Try removing the parameter $row from the function definition, and put it inside like this:

<?php 
function loggedInUsername() {
	require_once'connect.php';

$sql= "SELECT username FROM users WHERE id = " . $_SESSION['user_id'];
$stmt = $pdo->query($sql); 
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row;
}
 ?>

Just a suggestion, and I could be wrong, or it may not matter; store username as variable in session so you don’t query database on each page load.

2 Likes

And as you’ve gone to the trouble of using PDO for your query, have a look at prepared statements rather than just appending the session variable to the query string:

function loggedInUsername() { 
  require_once 'connect.php';
  $sql = "select username from users where id = :id";
  $prep = $pdo->prepare($sql); // prepare the query
  $prep->bindParam(':id', $_SESSION['user_id']); // assign the parameter
  $result = $prep->execute(); // execute the query
  $username = $prep->fetchColumn(); // only one column produced by query
  return $username;
}

$user = loggedInUsername();
echo $user;

It’s a good habit to get into, while it won’t make a massive amount of difference to this specific operation.

Another thing you need in that function, though, is some code to deal with what happens if the user_id isn’t found in your users table. Probably return false in that case, and deal with that result when you call it.

1 Like

A post was merged into an existing topic: Using header to move between pages

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.