aim0d
aim0d

Reputation: 129

Change user image based on who's logged in

I'm currently working on a login portal.

I already set the log in and log out functions and placed a session start button on every page.

My problem now is that I would like to update the user image based on the identity of who is logged in.

I'm using a template my boss chose, 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.

My project is in an 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"] represents the current user logged in. However, it doesn't do anything, not even producing an error.

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

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

Upvotes: 0

Views: 454

Answers (1)

LeftBrain
LeftBrain

Reputation: 346

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.

Upvotes: 1

Related Questions