Home > Blockchain >  Change user image based on who's logged in
Change user image based on who's logged in

Time:04-02

I'm currently working on a project for my workplace. It's a portal which every employee can log into.

I already set the logged in and log out function and placed session_start() in every page. My problem now is that I would like to update the user image, based on who is currently logged in.

I'm using a template my boss choose and this is the code where the image is:

<span>
    <img src="../../global/portraits/5.jpg" alt="...">
 </span>

Just a simple img tag with the link where the image is This is how it displays: enter image description here

My project is in a HTML file with PHP implemented and I tried to do this in the span tag, instead of img:

<?php
        $W = ["Raffaella", "Milena", "Domiziana"];
        $M = ["Umberto", "Domenico"];
    
        if ($_SESSION["nome"] == $W) {
                echo '<img src="W.jpg">';
        } else if ($_SESSION["nome"] == $M){
                echo '<img src="M.jpg">';
        }
?>

where $_SESSION["nome"] reppresents the current user logged in. But it doesn't do anything, not even an error.

Can someone explain how can i do it correctly, please? I'm new to php and trying to understand by studing alone on the internet

I was also considering Javascript for this work (instead of PHP) but don't know where to start.

CodePudding user response:

It looks like both $W and $M are array of names, rather than a single user, if that's the case, I assume you're trying to determine if a user is in that array. In PHP, you can use:

in_array($_SESSION["nome"], $W)

https://www.php.net/manual/en/function.in-array.php

which will return true/false.

        if (true === in_array($_SESSION["nome"], $W)) {
                echo '<img src="W.jpg">';
        } else if (true === in_array($_SESSION["nome"], $M){
                echo '<img src="M.jpg">';
        }

But, this only gives two separate images for up to 5 unique names, and I'm not sure if that's what you're going for.

  • Related