doujing5726 2013-01-14 01:29 采纳率: 0%
浏览 48
已采纳

使用PHP会话更改更改html菜单项

I'm trying to create a session once a user logs in so that the 'log in' menu item changes to 'log out' for the duration they are logged in. Once logged in, my success.php file runs which is just

<?php
    session_start();
    $_SESSION['loggedin'] = 1;
?>

Now under the menu I have

<?php
if($_SESSION['loggedin']=1)
echo '<a href="logout.php">Logout</a>';
else
echo '<a href="login.html">Login</a>';
?>

I also have a logout page which is just

<?php
    session_start();
    session_destroy();
?>

What's happening here though is that by default, the logout option is showing rather than log in even though the user hasn't yet logged in to create the session.

I'm not sure if this is the correct way of handling this, but some advice is very much appreciated.

Thanks.

  • 写回答

3条回答 默认 最新

  • dt3999 2013-01-14 01:31
    关注

    For login:

    if username and password is ok {
    
    session_start();
    $_SESSION['loggedin'] = "something";
    
    }
    

    For the menu

      if(isset($_SESSION['loggedin'])) {
        echo "<a href="logout.php"> logout </a>";
        } else {
    
        echo "<a href="login.php"> login </a>";
        }
    

    Ok, in order for the session to be remembered as you change pages, you should create a file called session.php and store following code.

    <?php 
    
        session_start();
    
         if(isset($_SESSION['loggedin']) && !empty($_SESSION['loggedin'])) {
    
         return true;
         }else {
         return false;
         }
         }
    
     ?>
    

    Now, include this page in every one of your .php pages

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

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)