Random stupid annoying error [FIXED]

The website url is https://www.bloxoria-2d.xyz theres a issue that it keeps getting: Warning : Cannot modify header information - headers already sent by (output started at /home/vol2_8/infinityfree.com/if0_34932503/bloxoria-2d.rf.gd/htdocs/announcement.php:15) in /home/vol2_8/infinityfree.com/if0_34932503/bloxoria-2d.rf.gd/htdocs/functions.php on line 140

EVEN THO NOTHING CAUSES IT if i remove my announcement code it stops appearing if i put the code back it causes errors and if i change it in the announcement table to hide it doesn’t cause anything so is it the database or infinity free being broken

 <?php
include("connection.php");
$query = "SELECT announcement_text, color, is_announcement_hidden FROM SiteSystem WHERE id = ?";
$stmt = $pdo->prepare($query);
if ($stmt) {
    $stmt->execute([1]);
    if ($stmt->rowCount() > 0) {
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        $announcementText = $row['announcement_text'];
        $announcementColor = $row['color'];
        $isAnnouncementHidden = $row['is_announcement_hidden'];
        if (!$isAnnouncementHidden) {
            echo '<div class="alert alert-' . $announcementColor . '" role="alert" style="max-width: 90%; margin: auto; margin-top: 10px;">';
            echo $announcementText;
            echo '</div>';
        }
    }
}
?>

I tried to remove style=“max-width: 90%; margin: auto; margin-top: 10px;”
it worked but i still don’t know whats the reason this warning happens

Things such as session_start() must come before all of the html tags used in the php code, failure to do such will result in the error that you are seeing :slight_smile:

7 Likes

Parse error : syntax error, unexpected variable “$query” in /home/vol2_8/infinityfree.com/if0_34932503/bloxoria-2d.rf.gd/htdocs/announcement.php on line 4

Read more: https://www.bloxoria-2d.xyz/home#ixzz8Ht6SYxmv

announcement is being included by navbar wich already has it

It means that you are missing a semicolon, you can try to look up the causes of these errors on Google and stackoverflow. You need to learn debugging in order to code better.

6 Likes

i fixed it with removing the style stuff and I’m gonna fix other pages who were affected by something else

Hi Danyal_Assi,

To achieve a better coding style, you may want to try the following techniques and format your code in a more formatted manner (not just by IDE). This enables faster debugging and less chance in encountering errors.

Assuming the script is only responsible for outputting the announcements:

<?php

include_once("connection.php");

$query = "SELECT announcement_text, color, is_announcement_hidden FROM SiteSystem WHERE id = ?";
$stmt = $pdo->prepare($query);
if (!$stmt) {
    return;
}

$stmt->execute([1]);
if ($stmt->rowCount() > 0) {
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
}

if(!$row){
    return;
}

$content = trim($row['announcement_text']);
$color = trim($row['color']);
if (!!!$row['is_announcement_hidden'] && !empty($content)) {
    echo sprintf('<div class="alert alert-%s" role="alert" style="max-width: 90%; margin: auto; margin-top: 10px;">%s</div>', $color, $content);
}

Here we have ensured that the database is only connected once and the connection is reused.

In case the statement is problematic, or there are no rows returned, the PHP exits out to the parent script so that this part does not block subsequent operations.

With easier variable naming and trimming, it’s easier for us to perform some logical checking before proceeding to formatting the alert.

If the message content is empty, regardless of the colour, we need not have this element in the UI, so instead of just checking for hidden status, check the content. The !!! syntax allows for fast conversion from mixed to boolean.

The code looks much cleaner and easier to read this way.

Cheers!

5 Likes

Maybe there is a copy-pasting error here, but there is a space before the first <?php. Because this space is outside the PHP tags, it will be sent to the browser as part of the page output. That will cause the HTTP headers to be sent if they weren’t already.

7 Likes

when i removed ?> it worked so thanks!

Yep, that’s why many coding style guides recommend against having closing tags in files that only contain PHP code. If you have an editor that automatically adds a blank newline at the end of the file, that blank line will be shown in the browser.

6 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.