My question is if in this way you can pass an image that is found in the users table
We imagine that a simple query brings all the results we want in an array:
SELECT id, username, email, role, image FROM users WHERE id=1
The resulting array could be something like this:
$result = array(
'id' => 1,
'username' => 'myusername',
'email' => '[email protected]',
'role' => 'admin',
'image' => '/src/images/myimage.jpg'
);
It is interesting (recommendable) to maintain a good structure and order the session variables. As you advance in the development, you make more and more use of them and they tend to increase their quantity.
An example with a certain order would be something like this:
$_SESSION = array(
'user' => array(
'id' => $result['id'],
'username' => $result['username'],
'email' => $result['email'],
'role' => $result['role'],
'image' => $result['image'],
),
'data' => array(
'last_visit' => 9786767866,
'theme' => 'blue',
//...
),
//...
);
It would only be possible to recover them where you need them
$user = $_SESSION['user'];
$data = $_SESSION['data'];
$username = $user['username'];
$avatar = $user['image'];
And put them in the view where you need them (I have assumed that is user and avatar as an example).
<ul class="inline">
<li><?php echo $username; ?></li>
<li><img src="<?php echo $avatar; ?>" alt="Avatar <?php echo $username; ?>"</li>
</ul>