douzhi4991 2019-04-14 11:26
浏览 138
已采纳

如何根据用户是否登录显示或隐藏信息

I am trying to develop a website and I am having issues with trying to hide information based on if a user is logged in.

I am using mysqli as a database and have registered a test user. Unfortunately, when using echo it doesn't change what is seen.

I have this in the header to create the session:

<?php
require 'includes/dbh.inc.php';
session_start();
 ?>

and then after that, I am using this in the index.php to try and show or hide based on if they are logged in:

<?php
if (!isset($_SESSION['userid'])) {
echo '<p class="login-status">You are logged in!</p>';
}

else {
echo '<p class="login-status">You are logged out!</p>';
}
 ?>

Regardless of what I seem to do it always displays "you are logged in!"

  • 写回答

3条回答 默认 最新

  • dongzaocheng3214 2019-04-14 12:51
    关注

    You are using a different index in login.inc.php and index.php. Try this in your index.php and it should work:

    <?php
    require 'header.php';
     ?>
    
    <main>
      <div class="wrapper-main">
    <section class="section-default">
    
    <?php
    print_r($_SESSION);
    if (isset($_SESSION['idusers'])) {
    echo '<p class="login-status">You are logged in!</p>';
    }
    
    else {
    echo '<p class="login-status">You are logged out!</p>';
    }
     ?>
    
    </section>
    </div>
    </main>
    
     <?php
    require 'footer.php';
      ?>
    

    I tried your code locally and it is working now.

    EDIT: I copied my working code from your index.php. Just replace it in your index.php file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大