?
Solved

Change a Login link to Logout link using PHP

Posted on 2007-10-14
7
Medium Priority
?
292 Views
Last Modified: 2012-05-05
Haven't been able to get this to work.

I simply want a "Login" link to appear at the top of all of my pages. When user sucessfully logs in this link turns to "Logout". AND displays their "username".

What code do I use (don't want to rely on cookies) and where do I place it? Do I insert code on ALL of my pages?  My pages consist of "Login Registration"; "Login", "Logout".  They all work fine.

Thanks!
0
Comment
Question by:geist62
  • 4
  • 2
7 Comments
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 20074498
after sucessfull verifcation of usrname and pass, you set a session say username

<?php
session_start();
if(isset($_SESSION['username']) && $_SESSION['username']!="")
echo("Login");
else
echo("Logout");?>
0
 
LVL 28

Expert Comment

by:gamebits
ID: 20074502
You must keep track somehow of who is loggedin, so the basic is

if (isset($_COOKIE['USERNAME']) && isset($_COOKIE['PASSWORD']))
    {
     echo "<A HREF=\"logout.php\">Logout</a>
    }
      else
    {
     echo "<A HREF=\"login.php\">Login</a>
     }

Of course you could use session as well.
0
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 20074511
sorry, interchanged login and logout in above code

and if there is link you can try something like

<?php
session_start();
if(isset($_SESSION['username']) && $_SESSION['username']!="")
{
  echo("<a href='logout.php'>Logout</a><br />");
  echo($_SESSION['username']);// displays username
}
else
{
  echo("Login");
}
?>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:geist62
ID: 20074632
Thanks! What I'm trying to understand is where to place your code kiranvj.  If I want the link on every web page do I simply insert it a the top of every page?

Regards!
0
 
LVL 16

Accepted Solution

by:
Kiran Paul VJ earned 375 total points
ID: 20074646
Paste the code where you have the current Login link

say this ur code link to login
<a href='somepage.php'>Some Link</a>
<a href='login.php'>Login</a>
<a href='some_other_link.php'>Some other link</a>

you new code will look like

<a href='somepage.php'>Some Link</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'>Some other link</a>

and add this on top of your page
<?php
session_start();
?>
0
 
LVL 28

Assisted Solution

by:gamebits
gamebits earned 375 total points
ID: 20074649
Yes, but you could put it in a file and include the file on every pages where you want the link to appear.

Gamebits
0
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 20074665
@geist62,
you can also do as gamebits mentioned

so ur code will look like

<a href='somepage.php'>Some Link</a>
include('login_logout.php');
<a href='some_other_link.php'>Some other link</a>

//login_logout.php
<?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> ");
}
?>

if you include the code as a file you dont need to change it in all files if there is a change in code.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question