dousong3760 2014-02-18 15:02
浏览 21
已采纳

会话变量未传递给下一个文件

So I've searched this site about this issue and tried what has been suggested and still no luck. I thought maybe it was my 'server' (On my tablet using KSWEB, no computer right now) so I created 2 simple files to share a session variable between the two and it worked fine. I have no idea why this isn't working for these two. I'm trying to create a login page (an insecure one, I know). The error function USED to work (this is what gets me), and now it doesn't. The files are below. I only included the top portion of admin.php because I've commented out the rest. It really shouldn't matter. Right now, if you submit the form without entering anything into the admin prompt, an error should display next to the asterisk saying "Admin needs to be filled out". Despite my best efforts, this doesn't work anymore and I'm completely stumped as to why.

Login.php

<?php 
session_start();
?>

<html>
<head>
  <style>
  .error {color: #FF0000;}
  </style>

  <script>
  function submitForm()
  {
  document.adminform.submit();
  }
  </script>
</head>

<?php echo $_SESSION["adminErr"];?>

<h2>Administrator login page</h2>
<form method="post" action="admin.php" name="adminform">
  Admin: <input type="text" name="admin" style="position:absolute; left:100px">
  <span class="error" style="position:absolute; left:285px">*<?php echo $_SESSION["adminErr"];?></span>
  <br><br>
  Password: <input type="password" name="password" style="position:absolute; left:100px">
  <span class="error" style="position:absolute; left:285px">*<?php echo $_SESSION["passwordErr"];?></span>
  <br><br>
  <button onclick="submitForm()">Submit</button>
</form>

<br><br><br>
<p><?php echo $_SESSION["flogin"];?></p>

</html>

<?php
session_destroy();
?>

Admin.php

<?php

session_start();

if ($_SERVER["REQUEST_METHOD"] == POST)
{
  if (empty($_POST["admin"])) // Check to make sure Admin field is filled out
    {
    $_SESSION["adminErr"] = "Admin field must filled"; // Set error if not filled
    header("location:login.php"); // Return to login page
    }
}
?>
  • 写回答

2条回答 默认 最新

  • douchensou6969 2014-02-18 15:12
    关注

    Don't destroy the session at the end of the file..

    </html>
    
    <?php
    session_destroy();
    ?>
    

    Also you should put exit; after each header('Location: ...');.

    When sending the header, the browser recognized to change the location but the script does not end. The browser in fact, would not even have to follow the header, it can also just go on with the script. You have to stop the script because the headers do not exit the script.

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?