doutuo8800 2019-03-30 06:59
浏览 81
已采纳

代码不能引用我之前设置的会话(PHP 7.2)

I am using sessions to send a success or error message to the next site where I use an include php file to show that message.

because of any reason, the message.php file isn't able to refer to the sessions I set before.

For example: The login script sets sessions depending whether the login was successful or not. Then it redirects you to the next page, where I use message.php as an include to every site I have. This is supposed to show an message on every site, when sessions got set before.

<?php
session_start();

if (isset($_SESSION['msgType']) AND isset($_SESSION['msgMessage'])) {
  if ($_SESSION['msgType'] != "" AND $_SESSION['msgMessage'] != "") {

    if ($_SESSION['msgType'] == "error") {
      ?>
      <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <strong>Fehler!</strong> <?php echo $_SESSION['msgMessage']; ?>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <?php
    } elseif ($_SESSION['msgType'] == "warning") {
      ?>
      <div class="alert alert-warning alert-dismissible fade show" role="alert">
        <strong>Warnung!</strong> <?php echo $_SESSION['msgMessage']; ?>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <?php
    } elseif ($_SESSION['msgType'] == "success") {
      ?>
      <div class="alert alert-success alert-dismissible fade show" role="alert">
        <strong>Erfolg!</strong> <?php echo $_SESSION['msgMessage']; ?>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <?php
    }
  }
}

$_SESSION['msgType'] = "";
$_SESSION['msgMessage'] = "";
?>
<?php
session_start();

$_SESSION['msgType'] = "success";
$_SESSION['msgMessage'] = "some message";

?> <script> window.location = "some URL"; </script> <?php
exit;

The first code is the message.php file where sessions are translated in messages. The second code is a piece of the login script. Where a session is set and you get redirected.

Because of any reason no message gets displayed at all. Can you see any problem in my code?

Also, if someone can tell me, why I can't use header('Location: URL') for redirection and have to use <script> window.location = "some URL"; </script> instead, just let me know pls.

  • 写回答

2条回答 默认 最新

  • douyanlu7380 2019-03-31 02:59
    关注

    The default PHP session handling is to use cookies via a header. The fact that you get an error when trying to set a Location header indicates that outputs already have been done.

    Ensure your script starts with <?php at the first character without any whitespace before. Also make sure your editor does not store a BOM (byte order mark) at the start of the file.

    See also: https://en.wikipedia.org/wiki/Byte_order_mark

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?