dqt20140129 2014-05-19 02:49
浏览 60

显示已登录用户的用户名以及其他人的注册/登录链接

I really need help for my login script. I want to show some PHP codes to show username for logged-in users else for others show only login/signup link.

Codes given below.

/*---Logged in user's homepage---*/


<?PHP

require_once('../lib/connections/db.php');

include('../lib/functions/functions.php');

checkLogin('2');

$getuser = getUserRecords($_SESSION['user_id']);

?>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title><?=$getuser[0]['username'];?>'s Profile Page | HDS Web</title>

<?php include('../headother.php'); ?>

<div style="background:#FFFFFF; padding-bottom:150px; padding-top:15px;">


/*----User's photo and details section goes below----*/


<div style="float:left; width:600px;">  

<p>
Welcome <?php if(empty($getuser[0]['first_name']) || empty($getuser[0]['last_name'])){echo $getuser[0]['username'];} else {echo $getuser[0]['first_name']." ".$getuser[0]['last_name'];} ?></p>                       

 <? displayUserImg($getuser[0]['id']); ?>

</div>


/*---User's navigation menu section goes below---*/


<div style="float:right; width:200px;"> 

    <a href="index.php">Home</a> | <? if (!empty($getuser[0]['thumb_path'])){echo "<a href='manage_photo.php'>Manage My Photo</a> | ";} else {echo "<a href='upload_photo.php'>Upload Photo</a> | ";} ?>

<a href="change_pass.php">change password</a> | 

<a href="edit_profile.php">Edit Profile</a> 

| <a href="log_off.php?action=logoff">sign out</a></div></td>


    </div>
    <?php include('../footer.php');?>


</body>
</html>
  • 写回答

1条回答 默认 最新

  • dtujfmfs06058 2014-05-19 03:40
    关注

    You can do like this:

    if(isset($_SESSION['user_id'])) {
      // detected a valid user
      ?>
      <p>Welcome <?php if(empty($getuser[0]['first_name']) || empty($getuser[0]['last_name'])){echo $getuser[0]['username'];} else {echo $getuser[0]['first_name']." ".$getuser[0]['last_name'];} ?></p>
      <?php                  
      displayUserImg($getuser[0]['id']);
    } else {
      // didn't login, show login link
      echo '<p><a href="login.php">Login Link</a></p>';
    }
    

    Mentioned in comment, checking $_SESSION['user_id'] does not guarantee accurate result, as the function may not return a user object when the user_id is not found in database. It is better to determine from the return value of getUserRecords().

    Side Notes:

    • Where is your DOCTYPE ?
    • Your HTML maybe invalid, without closing <head> and opening <body> tags (well, it may be contained in your included files. Who knows?)
    • stop using inline CSS
    • recommend not to use <? . Use <?php instead
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看