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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!