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

Konrad Pilch
Konrad Pilch
2,435 Points

How to make it?

HI,

How do i make, so when the user is logged in , this code:

<ul class="nav navbar-nav pull-right" id="main-navbar-right">
                            <li>
                                <a href="" data-toggle="modal" data-target="#loginWindow">Log In</a>
                            </li>
                            <li>
                                <a href="" data-toggle="modal" data-target="#registerWindow">Sign Up</a>
                            </li>
                        </ul>

changes to the code i want ? i mean , i know i need to use the if statement for sure and else but what should e inside the () ?

2 Answers

Grace Kelly
Grace Kelly
33,990 Points

Hmm, without delving into your code I could give you the following example:

<ul class="nav_bar">
<li>Home</li>
<li>About</li>
<li>Products</li>
<?php if (isset($_SESSION["username"])) { ?> //if the user is logged in
                        <li><?php echo $username . "'s Profile"</li>
                    <?php } else {?> //if the user is not logged in
                    <li >Login</li>
                    <?php }?>
<ul>

The conditional statement determines whether the user sees a link to their profile, or a link to the login page

Konrad Pilch
Konrad Pilch
2,435 Points

Hmm, what if i put the file the piece of code into template and include it when the userr is logged in?

Konrad Pilch
Konrad Pilch
2,435 Points

I did this:

 <?php if (isset($_SESSION["username"])) { ?> 
                                    <?php include'template/loggedin-login.php'; ?>
                                <?php } else {?> 
                                    <?php include'template/index_login.php'; ?>
                            <?php }?>

Its wokring :D

i have no idea how ugly my code is, but its wokring at the moment : pp then when i understand it , i can upgrade i probably, im making a social network site so i can reffer it and upgrade it in my studies.

Grace Kelly
Grace Kelly
33,990 Points

Hi Konrad there are a number of different options you could use, if you are using sessions you could do something like the following:

<?php

session_start();

if(SERVER["REQUEST_METHOD"] === "POST") { //when the form is submitted

$username = $_POST["username"]; //set the username

$_SESSION["username"]= $username; //assign it to a session variable

}
if (isset($_SESSION["username"])) { //if the session variable is set
    echo "Hello " . $_SESSION["username"];
} else {
       echo "You must be log in to see this content";


}

?>

This is a very basic example of using the sessions but you get the idea, if you're not using sessions you could set a variable called loggedIn and set it to true when the user logs in, for example:

<?php

if(SERVER["REQUEST_METHOD"] === "POST") { //when the form is submitted

$username = $_POST["username"]; //set the username

if($username === "mike") { //check if it's equal to "mike"
$loggedIn = true;                 //set loggedIn to true
} else {
echo "your username is incorrect.";
$loggedIn = false;
}

}

if ($loggedIn === true) {  //if the user is logged in
    echo "Hello " . $username;
} else {
       echo "You must be log in to see this content";
}

Again this is another basic example of what you could do, hope that helps!!

Konrad Pilch
Konrad Pilch
2,435 Points

Umm,i dont quite get you, i mean i do understand this, but if i want to apply to this code, and chenge the register in the nav bar to show the user name and logout when the user is logged in, how do it do it? : p i mean, do i need to make a class user somehow? and if umm heres my loggedin whichi changed to user :

<?php

SESSION_START();

if(!isset($_SESSION['username'])){

    echo "Incorrect username and password combination";
    die("<br /><a href='index.php'>Go back </a>");

}

$username = $_SESSION['username'];
?>
<?php include 'template/includes/header.php' ?>

<!-- User Profile -->
<div class="container">

<?php echo "Hello <strong> $username </strong> you have successfully logged in."; ?>

<br />
<a href="logout.php">Log out</a>

</div>