Jump to content

Dynamic log in / log out links


BarneyJoe

Recommended Posts

This is hopefully an easy one - just trying to have a link that's to log in if no-one is logged in, and log out if they are.

 

From a bit of a search, I realise it should be :

 

<?php
if(isset($_SESSION['username'])) {
    echo "Logout";
} else {
    echo "Login";
}
?>

 

Can anyone help me out with the syntax / code to define "Logout" and "Login"

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/65057-dynamic-log-in-log-out-links/
Share on other sites

I now have some code :

 

<div id="navcontainer" align="center">
<ul id="navlist">
<li><a href="index.php">Home</a></li>
..
<li><?php
if(isset($_SESSION['username'])) {
echo "Logout";
} else 
{
echo "Login";
}
?>
</li>
</ul>
</div>

 

But presumably I need to define "Logout" and "Login" somewhere?

 

Where do I put the actual links?

 

I've been trying with this sort of thing, but with no joy :

 

<?php
$login="<a href="login.php">Login</a>";
$logout="<a href="logout.php">Logout</a>";
?>

I don't know where I'm going wrong with this - if I have :

 

<?php
if(($_SESSION["MM_username"]))
{
echo '<a href="logout.php>">Log Out</a>';
} else {
echo '<a href="login.php">Log In</a>';
}
?>

 

Shouldn't that be it?

 

The session variable seems OK, as it outputs OK with a

 

<?php echo $_SESSION['MM_Username'] ?>

 

I'm getting the Log In link OK, but it's not switching to Log Out when I log in.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.