My website welcome page has got a link to login and one to logout. I want to know how to only show the logout link when the user is logged in and the other way around?
index.php code:
<?php require_once('Connections/PAPCon.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
session_start();
}
// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);
$logoutGoTo = "login.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_PAPCon, $PAPCon);
$query_Users = "SELECT * FROM tbl_utilizadores";
$Users = mysql_query($query_Users, $PAPCon) or die(mysql_error());
$row_Users = mysql_fetch_assoc($Users);
$totalRows_Users = mysql_num_rows($Users);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>RelPro</title>
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
<style type="text/css">
#container #intro #pageHeader h1 {
font-size: 4em;
}
</style>
</head>
<body>
<div id="container">
<div id="intro">
<div id="pageHeader" style="padding: 20px 0 0 50px;">
<h1>RelPro</h1><h2> </h2>
<h2><span>by: Mário Honório 12ºPI Nº6</span></h2>
<h2><div style="position: absolute; left: 944px; top: 152px; height: 100px;"><img src="Logo PAP 2.png" alt="" width="230" height="187" /></div></h2>
</div>
<div id="preamble">
<h3><span>Acerca da RelPro</span></h3>
<p class="p1">Este website foi criado no âmbito do Curso Profissional de Técnico de Gestão de Equipamentos Informáticos de forma a avaliar toda a aprendizagem realizada. Foi concebido de forma a facilitar o acesso a relatórios de projectos de final de curso de várias áreas a todos os interessados. É um website de fácil acesso, simples design e produzido em linguagem PHP, MySQL e HTML.</p>
</div>
</div>
<div id="supportingText"></div>
<div id="footer"></div>
<div id="linkList">
<div id="linkList2">
<div id="lselect">
<h3 class="select"><span>Secções:</span></h3>
<ul>
<li>RelPro</li>
<li><a href="relatorios.php">Relatórios</a></li>
<li><a href="admin.php">Admin</a></li>
<li><a href="contactos.php">Contactos</a></li>
<li><a href="login.php">Login</a></li>
<li><a href="<?php echo $logoutAction ?>">Log out</a></li>
</ul>
</div></div>
</div>
</div>
</body>
</html>
<?php
mysql_free_result($Users);
?>
Here is a picture of the section menu where the links are:
Either hidden or disabled will do.
You can display it with checking if the session variable(s) is set. Example for only one session variable: