Hi,
How would i place the log out link in every pages just when the user has logged in?
| SitePoint Sponsor |




Hi,
How would i place the log out link in every pages just when the user has logged in?




Pawel Decowski (you should follow me on Twitter)




There is session variable.
I know the logic of doing this, just the actual technique is unknown.
Sure, something like this should do it.
PHP Code://--> Anywhere on page.
<?php
session_start();
if($_SESSION['auth']['loggedIn'] == true){
echo '<a href="http://www.yourserver.com/logOut.php" title="Log Out">Log Out</a>';
}
?>
//--> logOut.php
<?php
session_start();
if(isset($_SESSION['auth']))
{
unset($_SESSION['auth']);
}
header('Location: http://www.yourserver.com/');
exit;
?>
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.




what about logout link? I need the link to be available in every pages.
This probably involves some css, isn't it ?
Further more, I need the link to disappear as long as user logged out
The logout link will only show if the user is logged in, just place the top bit of code on each page/template you want the logout link to be displayed.
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


I am not sure if this is anymore any help but ill post it anyway in case op wanted to see how the link is handled using eg. GET method.
$_SERVER['SCRIPT_NAME'] references to the page you are at the current moment. And CSS has nothing to do with making logout/login script in php.PHP Code:// The link and when clicked it will set variable called $_GET['logout']
echo '<a href="http://www.yourserver.com/'. $_SERVER['SCRIPT_NAME'] .'?logout">Logout</a>';
// And then you check if $_GET['logout'] is set or not and if yes you set your
// logout/auth variable to something that you use to check if user is logged or not.
if (isset($_GET['logout']))
{
$_SESSION['logged'] = false;
}




Bookmarks