Change login link to logout link.

There are times when you have to change the login link in your webpage to a logout link when a user successfully logged in.

Here is a simple way to do it in PHP

Suppose your menu is

Link 1
Login
Link 2

the corresponding HTML code will look like

<a href='somepage.php'>Link 1</a>
<a href='login.php'>Login</a>
<a href='some_other_link.php'>Link 2</a>

After successful login most probably you will set a session. Say the name of the session variable is username
Then the code for the menu will look like

<a href='somepage.php'>Link 1</a>
<?php
if(isset($_SESSION['username']) && $_SESSION['username']!="")
{
      echo("<a href='logout.php'>Logout</a> ");
      echo($_SESSION['username']);// displays username
}
else
{
      echo("<a href='login.php'>Login</a> ");
}
?>
<a href='some_other_link.php'>Link 2</a>

and also make sure to add

<?php
    session_start();
?>

on top of your page.

This entry was posted in PHP. Bookmark the permalink.

7 Responses to Change login link to logout link.

  1. Dave says:

    This is just what I’ve been looking for for my site update! Thanks sooo much!!

  2. amin says:

    thank you

  3. Christopher says:

    I am VERY new to php and have no idea what the code would look like for the session variable for username, or if that’s even something you could post?

  4. mehr says:

    Hi,
    Thank you, this forum solve my hard problem,

  5. khalid says:

    Thanks alot

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>