Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics (Retired) PHP Conditionals & Loops Conditionals

frank colin
frank colin
10,488 Points

When a member logs into a membership website, is this the code that shows your name on display when your logged in?

For example: login in : Tom password: ***

Welcome Back Tom!

I'm confused over a real world situation using conditionals in PHP. Help!

2 Answers

There could be many elements that have to be figured out to really answer your questions and without a full understanding of the login system we probably cannot help you with the actual code. But this is how I would approach it on an abstract level.

Start with $user = null;

If the user logs in, then $user = $userName;. You would use the actual variable holding the user name.

Then:

<?php
if ($user != null) {
    echo "Hello, $user.";
} else {
    echo "Hello, guest.";
}
Codin - Codesmite
Codin - Codesmite
8,600 Points

I would recommend looking into PHP sessions.

http://php.net/manual/en/intro.session.php

You can either store session information via URL or by saving cookies to the users computer that will remember global values across your website untill cookies are deleted or you destroy/unset the session.

Don't forget to have a cookie consent on your website because of EU laws, if you go down the cookie route :)