0

The problem is that the PHP Header Location does not work.

header('Location: index.php?action=new-game-step-choose-type');

It used to work, until I added those 3 styles.

If I delete those 3 styles, the app works again.

Since when does CSS properties influence the PHP ?

I developed a web app.

The app was running just fine.

Then, I added some styles :

#faq-page div {
    margin: 5px;
    padding: 5px;
}

#register-form-page div {
    margin: 5px;
    padding: 5px;
}

#login-form-page div {
    margin: 5px;
    padding: 5px;
}

After adding those styles, the app fails to work.

The warning that I see is :

Warning: Cannot modify header information - headers already sent by (output started at C:\laragon\www\the-aviator-web\menu.php:11) in C:\laragon\www\the-aviator-web\actions\action-new-game.php on line 11

There is nothing fancy within the menu.php file :

<a href="index.php?action=main" class="menu">MAIN</a>
|
<?php
if (
    isset($_SESSION['user_id'])
) {

    //

?>
    <a href="index.php?action=new-game" class="menu">NEW GAME</a>
    |
    <a href="index.php?action=join-game" class="menu">JOIN GAME</a>
    |
    <a href="index.php?action=friends" class="menu">FRIENDS</a>
    |
    <a href="index.php?action=logout" class="menu">LOGOUT</a>
<?php

    //

} else {

    //

?>
    <a href="index.php?action=login-form" class="menu">LOGIN</a>
    |
    <a href="index.php?action=register-form" class="menu">REGISTER</a>
<?php

    //

}

//

?>
|
<a href="index.php?action=faq" class="menu">FAQ</a>

Nothing fancy within the action-new-game file :

<?php

if (
    !defined('APP_NAME')
) {
    die;
}

//echo __FILE__;

header('Location: index.php?action=new-game-step-choose-type');

The code should redirect the user, but it does not, because the CSS properties have some kind of strange influence and the php header code stops, it does not get triggered.

Also, nothing fancy within the files where I use those 3 styles :

<div id="register-form-page" class="color-fff">
...
</div>

I removed each of those 3 css properties and put them within the file where they are being used, and the code works again just fine :

<style>
    #register-form-page div {
        margin: 5px;
        padding: 5px;
    }
</style>

<div id="register-form-page" class="color-fff">
...
</div>

What's happening and why ?

Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100
  • 2
    Any browser output sends the headers, so you can't then do a header redirect once they've already been sent, as the message says. Either change the sequence, or look at output buffering. I can't quite see where you reference those CSS properties, but presumably you must somewhere, if they're influencing things. – droopsnoot Oct 27 '20 at 10:14
  • The question is : can css properties influence php ? because if i delete those 3 css properties, the code works just fine :)) – Ionut Flavius Pogacian Oct 27 '20 at 10:16
  • But, when you delete those css properties, is there any other css remaining? – droopsnoot Oct 27 '20 at 10:17
  • yes, i still have css ... some simple css – Ionut Flavius Pogacian Oct 27 '20 at 10:17
  • OK, no idea then. I've read more (or less) into the question. – droopsnoot Oct 27 '20 at 10:19
  • where are you trying to add css to your script. – Viswanath Polaki Oct 27 '20 at 10:24
  • in the head section, within a head script – Ionut Flavius Pogacian Oct 27 '20 at 10:28
  • this is where the latest working app can be accessed : https://the-aviator.codercodex.ro/ – Ionut Flavius Pogacian Oct 27 '20 at 10:29
  • The team is trying to figure out why the app fails if we add the css properties. – Ionut Flavius Pogacian Oct 27 '20 at 10:30
  • Quite simply, you need to make sure no other output is created before you send the header. That applies to CSS, HTML, JavaScript or anything else. Just ensure the logic which decides whether to send the header runs before anything else in your script which adds content. (If you're sending a location header, it makes no sense to send content before that anyway, it's a waste of effort because the redirect instruction will ensure the browser never displays it.) – ADyson Oct 27 '20 at 10:37
  • ADyson, i moved those 3 styles within the pages that are being used, the code works again ... why and what's happening ? How can a style influence the php code ? – Ionut Flavius Pogacian Oct 27 '20 at 10:40
  • I already explained. It's not about CSS specifically, it's about _any output from your script which occurs before a header is sent_ . You can't output content and then later output a header. HTTP simply doesn't work like that. The headers must come first in the response. Therefore if you try to use PHP to set a header after you've already output some content it will cause an error, because that's not allowed. – ADyson Oct 27 '20 at 11:25
  • ADyson, the output of content is happening. There is a lot of html content and css style being output. The question is why are those 3 css styles making the difference ? – Ionut Flavius Pogacian Oct 27 '20 at 11:58
  • 1
    They aren't. They can't be. You must be mistaken. _any_ content output before the header will cause this error. This is well known, well documented and easily testable. You must be mistaken about the sequence of output of your other content. There's no other explanation. – ADyson Oct 27 '20 at 12:19

0 Answers0