0

When user is logged in have to show logout otherwise it will show log in bar.... I am trying the bellow code :

<?php
if(session_start())
{
  echo "<a href='home.php'>Logout</a>";
}
else echo " <a href='index1.php'>Login/Register</a>";
?>

// But this is not working. Kindly help

  • 2
    Welcome to SO. Just to confirm, how isn't it working for you? What is displayed? – Jonnny Sep 25 '15 at 10:11
  • are you using session_destroy() or unset() at the time of logout – Happy Coding Sep 25 '15 at 10:13
  • No I have done the logout part. In the header menu if the user is not logged in it will show Login, Otherwise if he is logged in the header menu will be changed to logout – Anirban Ghosh Sep 25 '15 at 10:19
  • @AnirbanGhosh When I run you code I see "logout". Is that not what you want? I'm not sure what isn't working for you here? – Jonnny Sep 25 '15 at 10:21
  • Yes even the session is not start yet it's showing the true part of the if statement, that is the logout part. And this is the problem. If a user is not logged in the how can the session start? It should show login part at that time @Jonnny – Anirban Ghosh Sep 25 '15 at 10:24
  • `session_start()` activates php's session mechanism, i.e. looking for a session id in the request data and, if found, loading the respective session data from the server's store "into" $_SESSION. This does not include any "is this user logged in?" logic. While handling the request that actually "logs in" the user you'd have to set some session data like e.g. `$_SESSION['user']=...` and then in subsequent request test whether that _SESSION element is set or not. – VolkerK Sep 25 '15 at 10:26
  • 1
    http://stackoverflow.com/questions/9001702/php-session-destroy-on-log-out-button – Joe Sep 25 '15 at 10:28

1 Answers1

2

You are calling session_start() that will start the session. What you need to do is create the login script and then start the session. I would perhaps set a $_SESSION variable at this point like $_SESSION['logged_in'] = true. Then in the navigation header check for

if($_SESSION['logged_in'] === true){
   // do something
}

That would perhaps be a better way to handle this.

Jonnny
  • 4,939
  • 11
  • 63
  • 93
  • After your advise i have tried this piece of code sir : – Anirban Ghosh Sep 25 '15 at 10:36
  • Logout"; } else echo " Login/Register"; ?> – Anirban Ghosh Sep 25 '15 at 10:36
  • But this time after login also it is showing that false part... The true part is not working now – Anirban Ghosh Sep 25 '15 at 10:37
  • Do you have a separate login file? Every time you run that code it sets `$_SESSION['username'] = ''` which isn't a `true` value. You should separate your login functionality from this script – Jonnny Sep 25 '15 at 10:41
  • Yes the login part is in a different script. Should I add $_SESSION['logged_in'] === true in that login script and then include this into this header file? – Anirban Ghosh Sep 25 '15 at 10:47
  • it depends how you use the sessions and what you use it for. When they login call `start_session()`, also set `$_SESSION['username'] = true`. When you logout make sure you call `session_destroy()`, probably wouldn't hurt to before that `unset($_SESSION['username'])` – Jonnny Sep 25 '15 at 10:51
  • Yes sir all that login and log out functionalities are working in a proper way. I just want to implement the header part as flipkart does. When the user is logged in it will show the log out option and when he is not it will show login option. But the header part is having problem right now – Anirban Ghosh Sep 25 '15 at 10:56
  • if you destroy the session and unset the everything then it should work fine. Have you tried print_r($_SESSION) to see what is still set? – Jonnny Sep 25 '15 at 11:00
  • Yes sir it's absolutely fine when I am login out and destroying the session . Problem is with while i am logged in it's yet showing the login bar while it should show logout – Anirban Ghosh Sep 25 '15 at 11:02
  • if you are doing if($_SESSION['username'] == true) it can't work if you've unset and destroyed the session. can you paste bin code? – Jonnny Sep 25 '15 at 11:08
  • which one is bin? the login script? – Anirban Ghosh Sep 25 '15 at 15:13
  • Try both, then you can see why it's still being returned. Or better still, update your question with the relevant scripts – Jonnny Sep 25 '15 at 15:14