0

I want to save my website header into $nav. I got all of it working, except for the part where I want to echo the username of the user.

I've tried multiple things, such as setting $_SESSION['username']; to $user and then try to echo $user into $nav, but this didn't work.

I also tried to just echo $_SESSION['username'], but it is not possible to use ''s as it will cause interruptions.

I am sure $_SESSION['username']; is set because I used var_dump($_SESSION); to check what things were set.

Output of var_dump($_SESSION);: array(3) { ["success"]=> string(21) "You are now logged in" ["rank"]=> string(1) "3" ["username"]=> string(5) "admin" }

My current code:

$user = $_SESSION['username'];

$nav = '<li><a href="index.php" class="kk" style="color: white;"><font color="white"><i class="fas fa-home"></i><b> Home</b></font></a></li>
    <li style="float:right"><a href="/index.php" class="header" style="color: white;"><i class="fas fa-user"></i><b><?php echo $user; ?></b></a></li>';

I expect the output of $_SESSION['username'], but it doesn't give me any output at all.

1 Answer 1

1

You're trying to write PHP code inside of a string. Those <?php ?> brackets don't belong there, it's just a string literal. Simplify. You're already in PHP code, just concatenate the values you want to build your string.

$nav = '<li>....etc....<b>' . $user . '</b></a></li>';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.