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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类