I am trying to work on a page where after the user successfully logs in, their username which is assigned from $_SESSION
to be used as a link to another PHP page.
if (isset($_SESSION['id'])) { print 'Welcome <a href="user.php">$_SESSION["user_name"]</a>'; } else { changePage("login"); }
I got it mostly working (it redirects), but I can’t seem to figure out the session variable and how to pull the variable in to be used.
Advertisement
Answer
If you use double quotes "
you can do string interpolation and do quite cool things with curly braces {}
if (isset($_SESSION['id'])) { echo "Welcome <a href='user.php'>{$_SESSION["user_name"]}</a>"; } else { changePage("login"); }
*Note that I swap the "
for '
in 'user.php'
I’ve used echo
above but I believe it is the same as print
, it’s the double quotes that are important.
Another option using single quotes would be concatenation which is a fancy way of saying join various bits together. In that case, I would write it as so:
print 'Welcome <a href="user.php">' . $_SESSION["user_name"] . '</a>';