How to do a if logged in show logout link/profile link and if not show the login link, in php code I have tried but failed I have gotten the username to post but that is about it I am also still learning php. I have spent the last 4 days searching and cant find a solution except ones for drupal or wordpress which I dont use so any help would be massively appreciated :)

Recommended Answers

All 2 Replies

How do you track if user is logged in? It would help if you posted some code.

Usually users are tracked in session array so if you use this method, check the values there. something like:

// start the session (on the very top of the script)
session_start();

// if user is logged in echo the html for links
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {

    echo '<a href="logout.php">Logout</a>&nbsp;|&nbsp;';
    echo '<a href="profile.php">Profile</a>';
}

Thanks broj1 massive help finally done what I wanted so here is the code I needed for future purposes and in case any other users are wanting to do the same thing as me with some descriptions.

The session is "LoggedIn" from the login page so if they are logged in they will get the "Logout | yourusername" but if you aren't it will post the login link as a "login" text works correctly so if anyone wants to use or improve you can :)

<?php   
 // if user is logged in echo the html for links
if(isset($_SESSION['LoggedIn']) && $_SESSION['LoggedIn'] == true) {

    echo '<a href="logout.php">Logout</a>&nbsp;|&nbsp;';
    echo '<a href="user-settings.php">' . $_SESSION['Username'] . '</a>';
}
else 
{
 echo '<a href="login.php">Login</a>';
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.