Redirect with PHP [duplicate] - php

This question already has answers here:
PHP form to email data AND take the user to a "thank you" page [duplicate]
(5 answers)
How to fix "Headers already sent" error in PHP
(11 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
Is there any better way to redirect because I get this error:
Warning: Cannot modify header
information - headers already sent by
(output started at
C:\xampp\htdocs\falco\index.php:26) in
C:\xampp\htdocs\falco\classes\controller.php
on line 306
Very often by using header("Location: blablabla.php?id=3")
Is there any other way to redirect and not get this error? or maybe I am doing something wrong?
THANK YOU FOR YOUR TIME.

You have to do header changes before any content on your page (also whitespace I think). So place the header function at the very top of your page.

This is a warning, not an error. It occurs because the headers were already sent to the browser. Make sure, that you don't have any output in your PHP file before modifying the header. This includes echo, print_r and also whitespaces before your intial <?php tag.

Headers need to appear before the body of your response. Therefore, if you have anything echo'd (including whitespace) and then attempt to send a header, it will fail.
Leave output for the very last thing in PHP.

You are printing headers after you've printed something else. The first method is just what you're doing, but you will have to wait with printing anything else until you know whether you want to redirect or not. You can use the output buffering functions if you absolutely must print before that. ob_start at the beginning to "pause" printing, then print the header, then call ob_end_flush to print everything that was held back.
Second method is inserting this into the <head>, but this too is timing-specific - you can't just insert it anywhere in the document.
<meta http-equiv="refresh" content="0; url=http://www.example.com/"/>
Third method, stick this script anywhere - but this forces the client to have scripts allowed, or nothing happens:
<script> location.replace('http://example.com'); </script>

You must have some whitespace or other output before you call header() which is triggering this warning. See the manual:
Remember that header() must be called
before any actual output is sent,
either by normal HTML tags, blank
lines in a file, or from PHP. It is a
very common error to read code with
include(), or require(), functions, or
another file access function, and have
spaces or empty lines that are output
before header() is called. The same
problem exists when using a single
PHP/HTML file.

Related

How can headers be sent before the page is finished rendering? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Warning: Headers already sent” in PHP
During testing I ran into the "headers already sent" issue. But then I thought, how can this occur? One of the headers is Content-Length which is unknown until the entire PHP script is finished, how does it get around this issue?
You should use output buffering:
ob_start();
// write all your code here
header('Content-Length: ' . ob_get_length());
Output buffering gets flushed implicitly when reaching the end of your script
One thing you could try, I'm not sure about, is to leave off the header() call and see if PHP automatically sets the Content-Length for you.
See also: ob_start()
Edit
If you're talking about how PHP does it, it doesn't always write that header; once the output buffer is full it will flush it without setting an explicit length header.
See also: http://php.net/manual/en/outcontrol.configuration.php
You get this message when you output anything before setting the headers using header(). So, make sure you don't echo anything, there's no HTML, no whitespace... nothing at all before you set the header.
You have to send the headers before you send anything else to output.Once the script produces anything - be it a static part of the page or a dynamically generated output - PHP sends the headers followed by whatever was sent to output (unles you use buffering, mentioned in other answers).
Hence you have to start your file with <?php and make sure, that it doesn't contain even the BOM character some editors like to put at before the file contents proper.

header has error in php , why? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Cannot send session cookie - headers already sent
Headers already sent by PHP
My previous problem was for UTF-8 but now ! again my code generates this error :
Warning: Cannot modify header information - headers already sent by (output started at F:\xampp\htdocs\1\error_list.php:34) in F:\xampp\htdocs\1\error_list.php on line 75
why?
my code is :
header("location:list.php?msg=Please enter some username and password");
There are several things 'wrong' with your code:
Don't use shorttags. They aren't really wrong but I don't like them :)
I think your define should be: define('ADMIN', $_SESSION['name']);
You problem either comes up because there is already some html or anything (space) outputted to the browser. If you have saved the file as UTF-8 you should make sure it doesn't include a BOM
This happen when you send html code even a single space to the browser. If you have any html code before this php code, so that the problem.
Solution
To tell the browser to buffer everything except php codes, you need to use:
ob_start();
when browser read the the function, it will buffer everything and never send it to the browser until your php code has done its task.
EDIT
Well, If you have some html code before your php code, what do you wanna do? Is there any thing you can do?
This a workaround for the problem, why not???

[PHP]Warning: Cannot modify header information [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
I have a simple code:
header("Location: http://www.wp.pl/");
end this code return:
Warning: Cannot modify header information - headers already sent by (output started at /var/www/plik.php:1) in /var/www/plik.php on line 2
I don't have any BOM, whitespaces etc. before "php" declaration.
What's wrong?
Clearly, you do have something sent already, but you can get around this by wrapping the PHP script in ob_start() / ob_end_flush().
1) Is that the only content on your page?
2) Is that script being included on a different page?
This warning is because when you write anything to the file, that is not the header, you cannot write to the header anymore. The header tells the script where to put its output, and if the header has been modified after content is written, then there is not guarantee of where it should put that content (as I understand it)
If this is not the only content on the page, check if anything above it is throwing an error or displaying anything.
If this is being included on a different page, check if that other page might be displaying something or throwing a warning/error.
Double check that there are no spaces or new lines after the closing php tag ?>. If there is, those spaces or new-lines get output to the browser, and since there is already output, you can't modify the header. A good practice is to just never include the closing php tag in your php files to prevent this from happening.

PHP headers already sent [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 2 years ago.
Possible Duplicate:
Headers already sent by PHP
Getting the following error:
"Warning: Cannot modify header information - headers already sent by (output started at..."
for the following line:
echo '<center>Current Time is '. gmdate("H:i A") . ' GMT (Greenwich Mean Time or UTC)<br />';
If I comment it out it just throws up the error at the next echo statement. Thoughts on why PHP hates my echo statements so much?
Here is my include toward the bottom of the HTML:
<div id="saveCanForm" width="100%">
<?php include('savereport.php'); ?>
</div>
It's not the echo statements that are the problem. It looks like you have a header call somewhere later in the file, but you can't send headers once you output any text at all. You could either move the headers to the beginning of the script or alternatively use output buffering.
Because our echos are coming before you are sending the header which is not allowed. Ensure that header go before any of your output.
If you don't want to rearrange you can also use output buffering.
Headers are dealt with before there is any other output, so if you write something out then PHP can't properly send headers afterwards. At some point in your code you are giving HEAD instructions which hence fails. (There's technical reasons for this, like redirects and so forth)
Its not the echo which is the problem. It is most probably caused by a file that you have included in the .php file. Have you included a file at all? This will probably be at the top. If you included file statement is not at the top of the file make sure it is.

Warning: Cannot modify header information. Can't find error [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I'm using a login-script and on my developmachine it worked perfectly but when I moved it to my webhost it stopped working.
The code can be viewed here: http://beta.yapaste.com/fm
The error I get when I login is
Warning: Cannot modify header information - headers already sent by (output started at /*/*/*/*/*/*/login.php:10) in /*/*/*/*/*/*/inc/login.php on line 43
But I can't find the error.
Thanks for answers, Victor.
Edit: Found the error, I included the file that I've got on yapaste in another file so the php-code wasent first.
On line 10 of login.php something is sent to the browser (white space between ?> and <?PHP tags count), at which point headers must be sent to. This triggers a warning when you try to then add more headers after this, on line 43.
You need to have a look at line 10 and prevent the output being sent, or use output buffering - your previous host could have had this on by default which might explain why it no longer works...
use ob_start(); at entry of your script to start buffering, but as Simon said, propably there is some white space wich is sended to browser befor you try to change headers.
Remeber, that you can omit ending php tag if you don't have any output code after, That will secure you from white spaces at end of file.
Also check for format of your source files - if they are encoded in UTF-8 with BOM, there are some starting characters that could be not shown in editor at beginning of the file.

Categories