登录时隐藏登录链接 [英] Hide login link when logged in

查看:106
本文介绍了登录时隐藏登录链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站欢迎页面上有一个登录链接和一个注销链接.我想知道如何仅在用户登录时显示注销链接,反之亦然?

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代码:

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>&nbsp;</h2>
            <h2><span>by: M&aacute;rio Hon&oacute;rio 12&ordm;PI N&ordm;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 &acirc;mbito do Curso Profissional de T&eacute;cnico de Gest&atilde;o de Equipamentos Inform&aacute;ticos de forma a avaliar toda a aprendizagem realizada. Foi concebido de forma a facilitar o acesso a relat&oacute;rios de projectos de final de curso de v&aacute;rias &aacute;reas a todos os interessados. &Eacute; um website de f&aacute;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&ccedil;&otilde;es:</span></h3>
                <ul>
                    <li>RelPro</li>
                    <li><a href="relatorios.php">Relat&oacute;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:

隐藏或禁用都可以.

推荐答案

您可以通过检查是否设置了会话变量来显示它.仅一个会话变量的示例:

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>';

请勿使用不推荐使用的MySQL驱动程序.改用MySQLi(已改进).

Do not use MySQL driver as it is deprecated. Use MySQLi(mproved) instead.

这篇关于登录时隐藏登录链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆