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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongzhuji1042 2014-02-18 15:08
    关注

    instead of

    <button onclick="submitForm()">Submit</button>
    

    use

    <input type="submit" value="Submit">
    

    Then put a check before echoing

    <?php echo isset($_SESSION["adminErr"])? $_SESSION["adminErr"]: "not set" ;?>
    

    further debugging:

    var_dump($_POST);
    var_dump($_POST["admin"]);
    var_dump($_SESSION);
    var_dump($_SESSION["adminErr"]);
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在win10 64位的vs打包MSI文件,放到win7 64位上安装成可执行exe文件后,点击程序不运行。
  • ¥50 大众点评用户浏览/消费记录爬虫
  • ¥15 求制作一个个人网页,
  • ¥15 寻涂色内存脚本作者有项目有市场有资源.却技术
  • ¥15 蓝桥杯c51单片机问题
  • ¥15 ajax跨域问题请求修改代码
  • ¥15 python matplotlib
  • ¥15 短信测压+语音,有偿,必须用Python
  • ¥20 COCOS2DX的protobuf协议注册函数问题
  • ¥15 (标签-Pytorch|关键词-Stream)