• 0

[PHP] Cannot modify header information


Question

hey all,

Got a problem with some code ive written.

I wrote it on my PC using WAMP to test it. I then wanted to move it about and show a friend so i copied the files over to my memory stick which has XAMPP on it.

When i run the script in XAMPP i get this error

Warning: Cannot modify header information - headers already sent by (output started at M:\PC Tools\Portable Apps\Server\xampplite\htdocs\cinema-database\admin\index.php:8) in M:\PC Tools\Portable Apps\Server\xampplite\htdocs\cinema-database\functions.php on line 80

I've done some searching and it seems to be because im setting the header after ive output stuff (as the warning tells me :p), the problem is, even when i move the header() call to the top of the page, before any output, i get no warning output, but the code also doesnt work.

Here is the code its having problems with.

78 | //if the cookie does not exist, they are taken to the login screen
79 | {
80 | header("Location: http://" . $_SERVER['SERVER_NAME'] . "/cinema-database/admin/login.php?msg=2");
81 | }

I have seen solutions saying use ob_start(), but doing this doesnt seem to make a difference.

Any suggestions?

Thanks in advance

Link to comment
https://www.neowin.net/forum/topic/848292-php-cannot-modify-header-information/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Correct. Headers can't be sent past any output.

I really don't see a reason why the header won't work if it was the first line on the page. Try inserting an Exit; right after the header redirection line.

If all fails, try having a page with just the redirection, and nothing else. See if that works.

  • 0

Looks like you are calling the functions.php (which contains the Header command) from the index.php, which is probably outputting something since you're still getting the error.

You may want to look into ob_start() and ob_end_flush(), although it's better to redo your code so that it isn't needed.

  • 0
I really don't see a reason why the header won't work if it was the first line on the page. Try inserting an Exit; right after the header redirection line.

You should have an exit regardless, telnet ignores redirects so they'll be able to see the page without logging in. :p

  • 0

hey all, thanks for all the replies.

what i dont understand is it works fine when using WAMP?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cinema Database - Admin Section</title>
</head>
<body>
<?php
require("../functions.php");
logincheck();
?>

Thats the code of index.php

ok, so i just tried moving the code around again, and this time its working.

I'll just have to move my header() calls around a bit to make it all work.

why does it work in WAMP without any warning messages though?

  • 0

As Code.Red briefly mentioned, your answer is ob_start. I'll be honest and I say I've only skimmed through the replies here but put the following code at the very beginning of your file with nothing else before it and it will work without having to move code around etc :D

<?php ob_start(); ?>

:)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • AMAZON needs to take total accountability for this.
    • Server Summit had a heap of announcements, ADCS changes are baller.
    • Nice, hope they *finally* fixed the issue with the NTFS driver where the system would completely brick during large file copies using the built in driver. It's been broken for years requiring me to use the older, slower, NTFS-3G FUSE driver.
    • Windows 11 KB5094126 BSODing, freezing, forcing BitLocker lockout, breaks OneDrive, and more by Sayan Sen Microsoft released Windows 11 KB5094126 and KB5093998 last week as the latest Patch Tuesday updates. Following that the company also published the accompanying dynamic updates under KB5094149, KB5095971, and KB5094156. While Microsoft has so far not acknowledged any major problems with the release, some users online are running into problems. These range from OneDrive and Dropbox access issues, BitLocker recovery lockouts, to blue screens and BSODs. The most common one seems to be happening with HP systems wherein affected users say they hit 0xc0430001 BSOD (blue screen of death) error code after the KB5094126 update. We wonder if this could be related to the recent bug we covered on HP devices wherein the ongoing Secure Boot certificate updates are leading to similar issues. While we are not certain, users affected by this issue likely need to ensure that the boot.stl file is included on the installation media (such as a USB installer or ISO), if the above-mentioned dynamic updates are deployed. If this file is missing, computers may fail to boot from the installation media and could display the error 0xc0430001. This STL file is used by Secure Boot to verify that the boot files are trusted, so it must match the same Windows version and system architecture. To ensure the file is included, Microsoft recommends using the Update WinPE script, which automatically updates the image and handles the required files. Alternatively, you can manually copy the boot.stl file from the Windows\Boot\EFI folder on a Windows device and place it in the matching folder on your installation media before deploying the updated image. Aside from blue screening some users also note their systems have been freezing following the update. This could be happening to Lenovo PCs specifically. In the case of the OneDrive and Dropbox access issues, a user figured out that there could be a conflict with UAC. He explained: "Okay, so I did some digging, and in our environment KB5094126 breaks OneDrive and Dropbox in Explorer. I went through all our GPOs and found out that the combination of disabling UAC and having my user being a local admin breaks OneDrive in Explorer. ... If I enable UAC again, then it works, even with KB5094126 still installed." Hopefully, Microsoft will look into these issues. Source: Microsoft forum (link1, link2, link3, link4), Reddit (link1, link2, link3, link4)
    • It is when it's a desktop in my house though for a PC that's lightly used and not really important when it is. If it was a laptop, it would be a different story. The real solution is varied and begins starting at post #22 in that thread.
  • Recent Achievements

    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      508
    2. 2
      +Edouard
      198
    3. 3
      PsYcHoKiLLa
      138
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      80
  • Tell a friend

    Love Neowin? Tell a friend!