问题
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.
回答1:
You can display it with checking if the session variable(s) is set. Example for only one session variable:
if(isset($_SESSION['MM_Username']))
echo '<li><a href="' . $logoutAction . '">Log out</a></li>';
else
echo '<li><a href="login.php">Login</a></li>';
Do not use MySQL driver as it is deprecated. Use MySQLi(mproved) instead.
回答2:
display it based on session
if($_SESSION['MM_Username'])
{
?>
<li><a href="<?php echo $logoutAction ?>">Log out</a></li>
<?php
}else
{
?>
<li><a href="login.php">Login</a></li>
<?php
}
回答3:
The use of a ternary operator can be used effectively here.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
session_destroy(); // comment this out if it gives you trouble
// Comment this out to test and using your own method of user check.
$_SESSION['MM_Username'] = "John";
$login = "<li><a href=\"login.php\">Login</a></li>";
$logout = "<li><a href=\"$logoutAction\">Log out</a></li>";
$check_login = isset($_SESSION['MM_Username']) ? $logout : $login;
$logout : $login- Only one will echo depending on if the session is set or not.
Then replace this entire block:
<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>
with:
<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>
<?php echo $check_login; ?>
</ul>
<?php echo $check_login; ?>takes care of both actions.- You will need to make a few adjustments of course, as far as placement goes.
Reference(s):
- http://php.net/manual/en/language.operators.comparison.php
回答4:
After some research and some try and fail attempts here is the final code i used to do this thingy (with some addons):
<?php
$loginlink = "index.php";
if (isset($_SESSION['MM_Username'])){
echo "Hello " . $_SESSION['MM_Username'];
echo "<br/><a href='".$logoutAction."'>Logout</a>";
}else{
echo "<a href='".$loginlink."'>Login</a>";}
?>
It worked for me and maybe it can help someone else. Thanks to @Richard Reiber whose code was my jump start.
Peace, Slaxer13
来源:https://stackoverflow.com/questions/30807893/hide-login-link-when-logged-in