dousheng3364 2017-01-04 13:54
浏览 144

HTML - 两个相同的代码片段运行不同的输出?

Just a fore note: I am messing around with HTML / CSS / PHP and MySQL. I do NOT have security in mind just yet.

I have 2 files, the first called page1.php and the second one called page2.php. Pretty simple, they both have a navbar which has some PHP code in it:

if (isset($_SESSION["loggedin"])){
    echo "<li class='navbar-button' id='right-status'><a>Logged in as admin</a></li>
";
    echo "<li class='navbar-button' id='right-change'><a href='logout.php'>Log Out</a></li>
";
} else {
    echo "<li class='navbar-button' id='right-change'><a href='login.html'>Log In</a></li>";
}

It checks whether or not a current session data named 'loggedin' exists, if it does it shows "Logged in as admin - Log Out" on the navbar. If it cannot find this session data, it shows "Log In" on the navbar. When I load page1.php, on the navbar it shows "Logged in as admin - Log Out", however when I load page2.php it shows "Log In". These both have identical code, and i m not sure why they produce different results. I'm not sure if it's relevant, but page2.php is not stored in the same folder as page1.php. Page1.php's directory is /page1.php. Page2.php's directory is /folder/page2.php.

Question 1: Do PHP sessions store data globally over every single file / folder in the entirety of the website? Or just in the root folder?

Question 2: Why does my code give different answers?

Thank you for your time in advance.

:)

Edit:

Sorry forgot to mention, page2.php was computer-generated using PHP.

  • 写回答

1条回答 默认 最新

  • doubing3662 2017-01-04 14:01
    关注

    It turns out I forgot to start the session in my page2.php (computer-generated) as it's quite complicated and I must've just skipped over it. Thanks to RossWilson who pointed it out.

    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据