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条)

报告相同问题?

悬赏问题

  • ¥15 关于将inet引入的相关问题
  • ¥15 关于一个倒计时的操作和显示设计
  • ¥15 提问STK的问题,哪位航天领域的同学会啊
  • ¥15 苹果系统的mac m1芯片的笔记本使用ce修改器使用不了
  • ¥15 单相逆变的电压电流双闭环中进行低通滤波PID算法改进
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 如何卸载arcgis 10.1 data reviewer for desktop
  • ¥15 共享文件夹会话中为什么会有WORKGROUP
  • ¥15 关于#python#的问题:使用ATL02数据解算光子脚点的坐标(操作系统-windows)
  • ¥115 关于#python#的问题:未加密前两个软件都可以打开,加密后只有A软件可打开,B软件可以打开但读取不了数据