Search code examples
phpfiletextmembersvisitors

display a section for logged in users


Basically i've been looking for a code to put into the index page. Where let's say your new, and you see a text that says

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

I've looked around, but no one seems to know. One came up, although it's from wordpress, and well i put it on my code, but i believe there something missing that i don't have. I just need a simple code, please you have one let me know

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>

Solution

  • So let's assume user logs in, and correct credentials are set.

    So we set a session with the username (in this case Script47). So something like this.

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

    There after, when on a "main" page (the page after login) have this.

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

    Don't forget to start session when setting session.