• 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.