显示登录用户的部分

display a section for logged in users

基本上我一直在寻找可以放入索引页的代码。让我们说你的新人,你会看到一条文字说

welcome visitor and then you login, and once you are it should say Hello .'user'.

我环顾四周,但似乎没有人知道。一个出现了,虽然它来自 wordpress,我把它放在我的代码中,但我相信我缺少一些我没有的东西。我只需要一个简单的代码,请你有一个让我知道

please explain the code and what I should change, cause sometimes i change things I shouldn't change and make things more complicated.

<?php
    session_start();
    include('acceso_db.php');
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
<style type="text/css">
<!--
body,td,th {
    color: #000000;
}
a:link {
    color: #333333;
    text-decoration: none;
}
a:visited {
    text-decoration: none;
    color: #333333;
}
a:hover {
    text-decoration: none;
    color: #CCCCFF;
}
a:active {
    text-decoration: none;
    color: #333333;
}
.style4 {
    font-family: 'EmmaSophiaRegular', 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', Lucida, Helvetica, Arial, sans-serif;
    font-size: 12px;
}
.style5 {
    font-family: 'EmmaSophiaRegular', 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', Lucida, Helvetica, Arial, sans-serif;
    font-size: 44px;
}
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}
.style6 {font-size: 33px}
-->
</style>
<script type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body onLoad="MM_preloadImages('potatos/3.jpg','potatos/wbr.png')">
<?php include_once("header_top.php"); ?>
<div id="pageMiddle" style="height:auto">
<table width="70%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td><div align="center">
            <!--SLIDESHOW CODE-->
            <a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image1','','potatos/3.jpg',1)"></a>
            <!--/SLIDESHOW CODE-->
            <a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('attack4','','potatos/wbr.png',1)"><img src="potatos/attack4.gif" alt="Dance!" name="attack4" width="500" height="278" border="0"></a></div></td>
        </tr>
  </table>
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="15">
        <td><div align="center" class="style5">Nugget's Club </div></td>
        <tr>
          <td>
          <?php
?>
          <?php
        if(isset($_SESSION['username'])) {
    ?>
            <div align="center"><span class="style6">Welcome: <a href="perfil.php?id=<?=$_SESSION['usuario_id']?>"><strong>
            <?=$_SESSION['username']?>
            </strong></a></span><a href="perfil.php?id=<?=$_SESSION['usuario_id']?>"><strong>           </strong></a><br />
          <a href="logout.php"></a>            </div></td>        
        <tr>
          <td></td>
  </table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="row"><table width="60%" border="0" cellspacing="25" cellpadding="0">
      <tr>
        <th scope="row">    <?php
        }else {
    ?>
            <a href="registro.php">Signup</a> | <a href="acceso.php">Login</a>
    <?php
        }
    ?> </th>
      </tr>
    </table></th>
  </tr>
</table>
</div>
<?php include_once("header_bottom.php"); ?>
</body>
</html>

The top part is the main page bottom part is the login page

       <?php
    session_start();
    include('acceso_db.php');
?>
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Login</title>
        <link rel="icon" href="favicon.ico" type="image/x-icon">
        <link rel="stylesheet" href="style/style.css">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css">
    <!--
    body {
        margin-left: 0px;
        margin-top: 0px;
        margin-right: 0px;
        margin-bottom: 0px;
    }
    -->
    </style></head>
    <body>
    <?php include_once("header_top.php"); ?>
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td><div align="center">
                      <table width="70%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td><div align="center"></div></td>
                        </tr>
                      </table>
                      <table width="70%" border="0" cellspacing="80" cellpadding="0">
                        <tr>
                          <td><div align="center">  <?php
            if(empty($_SESSION['username'])) { // comprobamos que las variables de sesión estén vacías        
        ?>
                <form action="comprobar.php" method="post">
                    READY TO LOGIN? <label>
                    <div align="left">Username</div>
                    </label>
                    <div align="left"><br />
                        <input type="text" name="username" />
                      <br />
                      </div>
                    <label>
                    <div align="left">Password</div>
                    </label>
                    <div align="left"><br />
                        <input type="password" name="usuario_clave" />
                      <br />
                        <a href="recuperar_contrasena.php">Lost Password</a><br />
                        <input type="submit" name="enviar" value="Ingresar" />
                      </div>
                </form>                    
                <div align="left">
                  <?php
            }else {
        ?>






              </div>
              <?php
?>
            <p align="left">Hello <strong><?=$_SESSION['username']?></strong> , are you already signed up? <a href="logout.php">Logout</a> and log back in. Maybe your wanting to <a href="registro.php">Signup</a>?</p>
            <p>
              <?php
        }
    ?>  
            </p></div></td>
                    </tr>
                  </table>
                  </div></td>
              </tr>
            </table>





<?php include_once("header_bottom.php"); ?>
</body>
</html>

I put it back to my own code, because I kind of got confused on where to put it. I put it on the top for both and it showed up although the welcome guest shows up all the way to the top. The code on the bottom is where i want the text to be.

              <?php
?>
            <p align="left">Hello <strong><?=$_SESSION['username']?></strong> , are you already signed up? <a href="logout.php">Logout</a> and log back in. Maybe your wanting to <a href="registro.php">Signup</a>?</p>
            <p>
              <?php
        }
    ?>  
            </p></div></td>
                    </tr>
                  </table>
                  </div></td>
              </tr>
            </table>

因此,让我们假设用户登录并设置了正确的凭据。

因此我们使用用户名(在本例中为 Script47)设置会话。所以像这样。

// Blah blah above user logs in...
// $username is the value of the login form. You have now set SESSION "username" to that value
session_start();
$_SESSION['username'] = $username;

然后,在"main"页面(登录后的页面)上有这个。

session_start();

if(!isset($_SESSION['username']) || empty($_SESSION['username'])) {
    echo 'Welcome Guest.'; // Your message here.
} else {
    echo 'Welcome ' . $_SESSION['username'];
}

设置会话时不要忘记启动会话