0

I have programmed a web application, which requires you to login first, before having access to the actual content of the application.

I am setting the value 'logedIn' to false when a user enters the homepage like this:

<?php
    $_SESSION['logedIn'] = "false";
?>

If the user tries to go to overview.php, the code checks if the value of logedIn equals 'true'. If it is not, a header relocates the window to the login page like this:

<?php
session_start();
?>

<html>
<head>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script src="https://jquery-ui.googlecode.com/svn-history/r3982/trunk/ui/i18n/jquery.ui.datepicker-nl.js"></script>
    <script src="../javascript/overview.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../css/mainpage.css">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

</head>
<body>

    <?php
        if($_SESSION['logedIn'] != "true") {
            header("Location: ../index.php");
            exit();
        }
    ?>

    <div class="container-fluid z-container">
        <div class="row z-overview-top-menu">
            <div class="col-md-2 z-background-dark z-top-nav z-border-right" id="menu_logo">
                <h3>Logo</h3>
            </div>
            <div class="col-md-2 z-top-nav z-item-hover z-border-right z-selected" id="menu_diary" onclick="changeMenu('diary')">
                <h3>Dagboek</h3>
            </div>
            <div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_pazo" onclick="changeMenu('pazo')">
                <h3>Pazo</h3>
            </div>
            <div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_counter" onclick="changeMenu('counter')">
                <h3>Tellers</h3>
            </div>
            <div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_overview" onclick="changeMenu('overview')">
                <h3>Overzicht</h3>
            </div>
            <div class="col-md-2 z-top-nav z-item-hover" id="menu_comparison" onclick="changeMenu('comparison')">
                <h3>Vergelijking</h3>
            </div>
        </div>
        <div class="row">
            <div class="col-md-2 z-overview-left-menu">
                <ul class="z-left-list" id="userList"></ul>
            </div>
            <div class="col-md-10 z-overview-main-menu">
                <ul id="overviewList" class="z-overview-list-main">
                </ul>
            </div>
        </div>
    </div>
</body>

When I replace the header with an echo, I get a response, indicating that the header is indeed executing when I run the code.

Also there are no .htaccess in any of the directories.

Any thoughts on what the problem might be?

zeno dhaene
  • 245
  • 3
  • 14
  • 1
    was that session started at all? you're also probably outputting before header – Funk Forty Niner Dec 23 '15 at 17:53
  • yes, on top of the overview.php, before the html tag, I will update the code – zeno dhaene Dec 23 '15 at 17:54
  • two things here. You're not checking for boolean, but strings in your session array. Probably why it's failing. `"true/false"` and `true/false` are 2 different animals. – Funk Forty Niner Dec 23 '15 at 17:55
  • indeed I am, I could do it with booleans, but I am doing it with strings right now. Could that be an issue? – zeno dhaene Dec 23 '15 at 17:57
  • yes that will be an issue as I don't see where you are using those strings. Plus, you say you started the session in one file, but was it started in the other? you only have `` – Funk Forty Niner Dec 23 '15 at 17:57
  • yes it has, in the exact same way as in overview.php, in front of the tag – zeno dhaene Dec 23 '15 at 17:58
  • also, when I print the value, I get "false" I removed the string and changed to a boolean, but still the same problem. – zeno dhaene Dec 23 '15 at 17:59
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Dec 23 '15 at 18:00
  • Doing what @Fred-ii- states will show the _Headers already sent_ error. – AbraCadaver Dec 23 '15 at 18:03
  • I slightly beat @AbraCadaver to closing it. But yes, the error from the duplicate will be the same as what I referenced from the php manual. – Devon Bessemer Dec 23 '15 at 18:04
  • It did indeed. Doing what @Devon said did the trick – zeno dhaene Dec 23 '15 at 18:04
  • I will accept the answer in 3 minutes. See, I think it is weird, because it always worked on my home xampp setup, but now that I'm migrating, it doesn't work anymore... – zeno dhaene Dec 23 '15 at 18:05
  • which is what I said in my [first comment](http://stackoverflow.com/questions/34441188/php-relocation-header-not-working#comment56623761_34441188) and anticipated as. *"you're also probably outputting before header"* but nothing from the OP about that. – Funk Forty Niner Dec 23 '15 at 18:06
  • I didn't know that defining styles and opening the tag was actually outputting something... – zeno dhaene Dec 23 '15 at 18:06
  • @Fred-ii- No need to be hearthbroken, I am very thankfull for your help, however, since they don't give it-lessons to an 18-years-old in my country, I have to figure this all out on myself, so if I don't understand what you are saying, that is my fault, and not yours. However, by explaining what you mean, you can really help me a lot more than talking to your own (since nobody is ever going to read this topic again). – zeno dhaene Dec 23 '15 at 18:18
  • @zenodhaene hahaha! I'm not "heartbroken". I see too many questions on Stack where people don't post their full/real code and I start guessing *right and left* as to what the problem could be, resulting in many comments. Had I known from the start, I would have been able to either provide you with a solution, or as a duplicate to the question which was indeed that, *outputting before header*. Welcome to *Coder's world*, where you have many tools at your disposal, a few of which that I have already given you. Enjoy, *cheers* and Happy Holidays ;-) – Funk Forty Niner Dec 23 '15 at 18:21

1 Answers1

1

http://php.net/manual/en/function.header.php

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Since you are outputting:

<html>
<head>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script src="https://jquery-ui.googlecode.com/svn-history/r3982/trunk/ui/i18n/jquery.ui.datepicker-nl.js"></script>
    <script src="../javascript/overview.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../css/mainpage.css">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

</head>
<body>

before the header call, it will fail.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • That fixed the problem! thank you very much! – zeno dhaene Dec 23 '15 at 18:03
  • which strangely enough is what I figured as much in [my very first comment](http://stackoverflow.com/questions/34441188/php-relocation-header-not-working#comment56623761_34441188) to the OP. – Funk Forty Niner Dec 23 '15 at 18:07
  • @zenodhaene http://php.net/manual/en/function.headers-sent.php --- http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Dec 23 '15 at 18:11
  • @Fred-ii-, yeah, looks like he edited the OP with more info between the two. Possibly he didn't expect html tags to be classified as "output". – Devon Bessemer Dec 23 '15 at 18:11
  • @Devon The `` wasn't part of their original post http://stackoverflow.com/revisions/34441188/1 - Had I known that from the start, I'd of closed it with the "now" duplicate. Hard to say as to what they're really using at this point. – Funk Forty Niner Dec 23 '15 at 18:12
  • @Devon exactly, thank you very much. – zeno dhaene Dec 23 '15 at 18:19