• 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

    • Anyway to download these versions without being on the Experimental builds?
    • Nothing is stopping you from continuing with your testing cadence. If updates are released every 2 weeks instead of 4, and you test once every 4 weeks, the exact same amount of patches will still be available for you in those 4 weeks. For example: Before 4th week - patch 1, 2, 3, 4 After 2nd week - patch 1 and 2 4th week - patch 3 and 4 Still the same amount after 4.
    • Everyone else has said it. I'm gonna say it - you don't know what you're talking about. I do. I have two laptops. One work, one personal. I have access to two more laptops - both personal. At home I manually update my personal laptop when I see on Neowin that there is an update - I carry on and only apply the updates when I am ready. My work one only updates when my workplace decides to send it - I carry on and only apply the updates (when they actually arrive, which is usually days after the release) when I switch off the laptop at the end of the day as usual. The two other personal laptops only get updated when I get to it which is rarely - the people who own them carry on using them until I get to it and update them. All of the browsers on all laptops are configured to restore the tabs when launched. Google and Microsoft have changed from 6 weeks to 4, and it looks like it's going to move to 2. None of these changes affect how any of these browsers on the laptops are used. Not one jot. My advice to you is stop panicking whenever you see an update. Just carry on with what you're doing. This even benefits you in a way - from your comment you sound like you don't like the changes or the frivolous new features - great - then carry on as before!
    • AMAZON needs to take total accountability for this.
    • Server Summit had a heap of announcements, ADCS changes are baller.
  • 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
      509
    2. 2
      +Edouard
      198
    3. 3
      PsYcHoKiLLa
      138
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!