0

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.

  • `!empty($var)` is equivalent to `isset($var) && $var != ""`. However, *both* are **not** equivalent to the *not identical* operator `!==`. `0 != ""` is `false`, `"0" != ""` is `true`, `0 !== ""` is `true`. See also the PHP 7 null-coalescing operator `??`. – Pinke Helga Mar 31 '19 at 03:07

2 Answers2

0

YES YOU CAN, use header('location : your_url')

<?php
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session')); //PUT THIS ON THE TOP
session_start();

Give it a try

Vipertecpro
  • 3,056
  • 2
  • 24
  • 43
  • I used header('Location: URL') before but It didn't work. I read that you can't use header() after any kind of output. Does setting a session counts as an output? –  Mar 30 '19 at 09:44
  • nothing. I just get a simple white page without any error message –  Mar 30 '19 at 12:07
  • Can you check on the page whether session is storing value after redirecting to another page using header() ? and why are you checking same thing two times here `if (isset($_SESSION['msgType']) AND isset($_SESSION['msgMessage'])) { if ($_SESSION['msgType'] != "" AND $_SESSION['msgMessage'] != "") {` ??? isset is enough to check and use && instead of AND take a look here https://stackoverflow.com/a/2803374/5928015 – Vipertecpro Mar 30 '19 at 12:26
  • the session is not storing any value at all. –  Mar 30 '19 at 17:26
0

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

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
  • thanks a lot. I found the error. I had an html comment in the first line, which obviously counts as an output:) –  Apr 05 '19 at 07:53