PHP Header redirect not working [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
include('header.php');
$name = $_POST['name'];
$score = $_POST['score'];
$dept = $_POST['dept'];
$MyDB->prep("INSERT INTO demo (`id`,`name`,`score`,`dept`, `date`) VALUES ('','$name','$score','$dept','$date')");
// Bind a value to our :id hook
// Produces: SELECT * FROM demo_table WHERE id = '23'
$MyDB->bind(':date', $date);
// Run the query
$MyDB->run();
header('Location:index.php');
exit;
The above code keeps giving me an issue with the redirect. The error is the following:
Warning: Cannot modify header information - headers already sent by (output
started at /Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15) in
/Applications/MAMP/htdocs/testygubbins/OO/test/form.php on line 16.
I am totally flummoxed by this. Does anyone know what I should be doing to make it work?
EDIT
header.php code:
<?php
include('class.user.php');
include('class.Connection.php');
$date = date('Y-m-j');
?>
<html>
<head>
<link rel=StyleSheet href="css/style.css" type="text/css" media=screen>
<title>Test</title>
</head>
<body>
<div id="page">

Look carefully at your includes - perhaps you have a blank line after a closing ?> ?
This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls.
Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.
(EDIT: looking at your header, you need to avoid doing any HTML output if you want to output headers, or use output buffering to capture it).
Finally, as the PHP manual page for header points out, you should really use full URLs to redirect:
Note: HTTP/1.1 requires an absolute
URI as argument to Location:
including the scheme, hostname and
absolute path, but some clients accept
relative URIs. You can usually use
$_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to
make an absolute URI from a relative
one yourself:

COMMON PROBLEMS:
1) There should be NO output (i.e. echo... or HTML parts) before the header(...); command.
2) After header(...); you must use exit();
3) Remove any white-space(or newline) before <?php and after ?> tags.
4) Check that php file (and also other .php files, that are included) -
they should have UTF8 without BOM encoding (and not just UTF-8). Because default UTF8 adds invisible character in the start of file (called "BOM"), so you should avoid that !!!!!!!!!!!
5) Use 301 or 302 reference:
header("location: http://example.com", true, 301 ); exit;
6) Turn on error reporting. And tell the error.
7) If none of above helps, use JAVASCRIPT redirection (however, discouraged method), may be the last chance in custom cases...:
echo "<script type='text/javascript'>window.top.location='http://website.com/';</script>"; exit;

Alternatively, not to think about a newline or space somewhere in the file, you can buffer the output. Basically, you call ob_start() at the very beginning of the file and ob_end_flush() at the end. You can find more details at php.net ob-start function description.
Edit:
If you use buffering, you can output HTML before and after header() function - buffering will then ignore the output and return only the redirection header.

Try This :
**ob_start();**
include('header.php');
$name = $_POST['name'];
$score = $_POST['score'];
$dept = $_POST['dept'];
$MyDB->prep("INSERT INTO demo (`id`,`name`,`score`,`dept`, `date`) VALUES ('','$name','$score','$dept','$date')");
// Bind a value to our :id hook
// Produces: SELECT * FROM demo_table WHERE id = '23'
$MyDB->bind(':date', $date);
// Run the query
$MyDB->run();
header('Location:index.php');
**ob_end_flush();**
exit;

Look at /Applications/MAMP/htdocs/testygubbins/OO/test/header.php line 15.
At that position, it makes some output. Fix it. :)

If I understand correctly, something has already sent out from header.php (maybe some HTML) so the headers have been set. You may need to recheck your header.php file for any part that may output HTML or spaces before your first
EDIT: I am now sure that it is caused from header.php since you have those HTML output. You can fix this by remove the "include('header.php');" line and copy the following code to your file instead.
include('class.user.php');
include('class.Connection.php');
$date = date('Y-m-j');

You may have some "plain text" somewhere in php files that is interpreted as script output. It may be even a newline before or after the php script tag specifier (less-than + question mark + "php").
Besides, if I remember correctly, according to http specification, the "Location" header accepts only full URLs, not relative locations. Have that in mind too.

Don't include header.php. You should not output HTML when you are going to redirect.
Make a new file, eg. "pre.php". Put this in it:
<?php
include('class.user.php');
include('class.Connection.php');
?>
Then in header.php, include that, in stead of including the two other files.
In form.php, include pre.php in stead of header.php.

Your include produces output, thereby making it impossible to send a http header later. Two option:
Move the output somewhere after the include.
Use output buffering, i.e. at the very start of your script, put ob_start(), and at the end, put ob_flush(). This enables PHP to first wait for all the output to be gathered, determine in what order to render it, and outputs it.
I would recommend you learn the second option, as it makes you far more flexible.

also try include_once() instead of include() that can also work

Also see your php file text encoding. Mine was UTF-8 with BOM and it prevented the script to work. But now works flawlessly after removing the BOM...

Try redirection with JavaScript:
<script type="text/javascript">
window.location.href='index.php';
</script>

Related

Cannot modify header information (connot detect header)

I checked all answers in different pages and use it, but the error still appears. Please help me.
The error is:
Warning: Cannot modify header information - headers already sent by (output started at /home/iraustor/public_html/copytest/post.php:1) in /home/iraustor/public_html/copytest/post.php on line 57
The URL of form is here: http://iraust.org/copytest/contact.html
And the page that after complete the form is: http://www.iraust.org/copytest/thanks.html (or any other method to shod this message)
It has taken 2 days but answer. Please help me.
"header("Location:$Redirect_Page");"
If you issue headers, like you do for a redirect (setting Location) you MUST be sure that there's no other output before that statement, as PHP will already build the headers (however maybe not yet flushing them to the client) on the first output.
This might be the case for several reasons (unexpected error in some require, a whitespace at the beginning or end of some file, etc, but the error message you have is clear in mentioning where this output started: /home/iraustor/public_html/copytest/post.php:1.
You should double check that there's nothing before the opening and after the closing <?php ... ?> block. This applies to all included or required files in called script.
As pointed out by h7r, if you use the header function you cannot print anything before its call.
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.
From Header documentation on PHP.net
So, the first thing you should check is the line 57 in your post.php file: there starts the output that causes the error.
Be sure that no output is sent: also a white space or a blank lines is an output and this cause the error.
If you like, you can use the output control functions to buffer the output: in this way you can print what you want but all your outputs aren't sent immediately to browser, so you can use the header function without causing errors.
Put your code somewhere for us to look at...
Possibly PHP could be outputting an error, or a warning, etc... It might not be you for example.
no space before
I completely confused!
The form is working and the information will be send to e-mail
but the error makes feeling nervous for users
The problem solved by simple code editor (notpad++)
problem: hidden white space and non Unicode characters

Header Location in php

<?php
require ('db_connect.php');
$Name = $_POST["name"]; $Email = $_POST["email"]; $Message = $_POST["message"];
if( isset($_POST["submit2"]) ) {
$insertString2 = "INSERT INTO Messages(Name,Email,Message)
VALUES('$Name','$Email','$Message')";
mysql_query($insertString2);
header("Location:register.php?msg=Successfully Send Message! You will get reply soon...");
}
?>
This is my code. The MySQL part is working. But this does not redirect to register.php. Why?
You may put a header call in an if-conditional preceded by other code, as long as you follow what the Manual recommends:
You can use output buffering ... by calling ob_start() and ob_end_flush() in your
script, or setting the output_buffering configuration directive on in your php.ini
or server configuration files.
If you wish to alter your PHP.INI file, you may turn output buffering on, as follows:
output_buffering = On
With output buffering turned on, this code structure should work:
Try this for your header call:
// setting variables here, then:
if (condition) {
// other code here, then:
$location = "http://yourdomain.whatever/register.php";
$encoded = urlencode("Successfully Send Message! You will get reply soon...");
header("Location: $location?msg=$encoded");
exit;
}
Note: when you use header() if you have any concern with maintaining backwards compatibility with http1.0, then you should provide a full url, including the protocol whether that is 'http', 'https' or something else. Also your query string contains characters that need to be urlencoded. After passing the value to be encoded to urlencode(), the string looks like this:
Successfully+Send+Message%21+You+will+get+reply+soon...
Part of the joy of using PHP is that it does nice things like automatically decoding the encoded url. So all you need do to display a message to the user is write something similar to the following code at register.php:
<?php echo htmlentities($_GET['msg']);
if you wish to decode the $_GET variable with JavaScript, just be aware that PHP and JavaScript do not urlencode spaces the same way, so you need to manually decode the $_GET variable's value yourself rather than fully relying on JavaScript's decodeURIComponent(). The following JavaScript will urldecode the value of "msg", including converting any '+' characters into spaces:
var str=location.search.substring(5);
str = decodeURIComponent( str );
str=str.replace(/\+/g, ' ');
alert(str); //Successfully Send Message! You will get reply soon...
"It is important that header() must
be called before any actual output is sent"--you may miss this point.

Jquery POST and AJAX prepend double space to returned data

I am still struggling with this problem after about 2 weeks with no sign of a solution.
Any data that is returned by PHP using $.ajax or $.post always has two spaces added onto the returned data. I have trimmed the data being echoed in PHP to confirm it's not an issue with the server or my scripts.
e.g.
echo '{"id": "'.$myId.'"}';
Becomes:
' {"id": "'.$myId.'"}'
When viewing the returned data in inspector. This causes problems for my js scripts because they expect nothing returned when there is no error. Double space is returned which causes errors when there are actually none, which in turn stops other events from firing.
I am using Jquery 1.8.3.
Does anyone have any idea what is causing this extremely strange and annoying issue?
I am using NetBeans
I recall that this only started happening since I moved my app to a new server, but I don't see how that would have effected it in this way.
This may be helpful,
I think there is a whitespace in your script (may be from included files, but not sure). You can overcome this by clearing the output buffer, before you send the data to browser. The following code will demonstrate the idea.
<?php
ob_start();
echo ' '; // Possible whitespace (may be from included files)
----------
----------
if (YOUR_CHECK_FOR_AJAX_REQUEST) {
ob_end_clean();
$myId = 1;
echo '{"id": "'.$myId.'"}';
exit;
}
?>
There is chance that you've got whitespaces at the end of your included php files (to prevent it, skip ending ?> tag).
Also you should check if there is nothing in front of <?php and disable BOM in your UTF-8 files.
The data which is returned from the file would have spaces or you would have html tags. Remove all the tags, lines, spaces and at the end remove ?>
If you are returning data from test.php the file should look like as below
<?php
$myId = '1';
echo '{"id": "'.$myId.'"}';

Problem with sending "SetCookie" first in php code

According to this manual: http://us2.php.net/setcookie I have to set the cookie before anything else.
Here is my cookie code:
if (isset($_COOKIE['watched_ads'])){
$expir = time()+1728000; //20 days
$ad_arr = unserialize($_COOKIE['watched_ads']);
$arr_elem = count($ad_arr);
if (in_array($ad_id, $ad_arr) == FALSE){
if ($arr_elem>10){
array_shift($ad_arr);
}
$ad_arr[]=$ad_id;
setcookie('watched_ads', serialize($ad_arr), $expir, '/');
}
}
else {
$expir = time()+1728000; //20 days
$ad_arr[] = $ad_id;
setcookie('watched_ads', serialize($ad_arr), $expir, '/');
}
As you can see I am using variables in setting the cookie.
The variables comes from a mysql_query and I have to do the query first.
But then, if I do, I will get an error message:
Cannot modify header information - headers already sent by ...
The error points to the line where I set the cookie above.
What should I do?
UPDATE:
I do this before the setCookie part:
$ad_id=$_GET['ad_id'];
$query2 = "SELECT * FROM classified WHERE classified.ad_id = '$ad_id'";
$results2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_array($results2);
$cat = $row2['category'];
$action=$row2['action'];
$sql_table='';
$num_rows = mysql_num_rows($query_results);
if ($num_rows != 0){
HERE COMES THE SETCOOKIE PART
As others have suggested, make sure, you are not outputting any html or whitespace before you set your cookie.
This will fail because you are printing html before you set your cookie.
<p>
<?php
// your cookie code - note <p> tag before <?php tag
// ...
?>
This will also fail, because you are printing whitespace before you set your cookie.
 
<?php
// your cookie code - note the extra linebreak before <?php tag
// ...
?>
Also
<?php
// your cookie code - note the extra space before <?php tag
// ...
?>
If you use an UTF encoding for your php script (and if you are not in one of the english speaking countries, chances are that you do), make sure your editor is set that it does not include byte order mark (BOM) at the begining of every file. See http://en.wikipedia.org/wiki/Byte_order_mark for more detail on BOM.
The restriction is not that you must not do anything before setting your cookies, merely that you must no output anything before setting your cookies.
For example, let's say we want to get some data from the database, output it to the user and set it to the cookie.
<?php
$data = getDbData();
echo $data['field'];
setcookie('field', $data['field'], time()+86400, '/');
This will fail because we've output the data before setting the cookie. We can fix it by moving the output to after we set the cookie.
<?php
$data = getDbData();
setcookie('field', $data['field'], time()+86400, '/');
echo $data['field'];
Make sure that you do not print anything prior to adding header-based information (as cookies are).
I can't see any problems with the code, unless mysql outputs an error, which could cause this.
This is a shot in the dark, but make sure you don't have any whitespace (or anything else for that matter) before the opening php tags. Also make sure you don't have any trailing whitespaces after the closing php tags in the files you include.
The error message you showed us says that the headers were sent on the setcookie() line. Thus, you may be setting headers or cookies later in the code, which is causing the error. (Or so I believe, since I can't recall the error word for word and you cut it off at the critical point)

PHP warning: headers already sent in Unknown [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I'm looking for things that might trigger the following PHP warning:
PHP Warning: Cannot modify header
information - headers already sent in
Unknown on line 0
Turned out that it was the line
ob_start("ob_gzhandler");
that caused the warning. This has been reported and fixed in 2001, it seems, but for some reason it keeps coming back.
It might be a lot of things, but as the others said, it's often just a space lying around somewhere that gets outputted and then a header() command is sent which normally is fine, but not after starting to send content back (potentially just a space in this case).
Using ob_start() stops the output from going out right away by buffering it. So it's a potential solution, or at least way to diagnose where it's coming from as zodeus said.
One common thing that causes those lose spaces are in this scenario.
global.php
<?php
$variable = 1;
$database = 'something else';
?> <-- A space here
<-- Or here
index.php
<?php
require('global.php');
$var = dosomething();
header('Location: http://www.example.com/');
?>
One way to fix that is to remove the ?> at the end of the global.php file. You don't need those, they are only useful if you start putting HTML for example after your PHP code. So you'd have:
<?php
$variable = 1;
$database = 'something else';
And when you do the require(), the space is not outputted before the header().
Just to illustrate the problems with content outputted and headers is that other common case that gives a similar error. It happens when you forget to stop the processing after a redirection using header().
if ($notLoggedIn) {
header('Location: http://www.example.com/login.php');
}
echo 'Welcome to my website'; // this will be outputted,
// you should have an exit()
// right after the header()
I think whats happening is one of the built in php functions is outputting something. I've seen this with a couple of the IMAP functions where they out put just a " " (space character) and it screws things up.
You can thry tracking it down using Xdebug or the Zend debugger, but i f you have neither
try wrapping output buffering around some of the functions you think may be cause it.
ob_start();
callYourFunction();
ob_end_clean();
Do this one function at a time and when the error goes away you'll know which function is cause you the problem, then you can either file a bug report or just leave it in as a hack. But at least then you know what function is cause the issue.
Edit: The fact that is says your output is occurring on line 0 means that it's a C level function doing the output, not any code that's been written using PHP.
Have you checked your files for unintended UTF-8 BOMs?
The error tells you that something has sent output, which would force headers to be sent, because the headers must be written before the body of the http message.
The most common problem I have found is text in headers. vis:
<?php // myfile.php
include 'header.php';
?>
and in header.php:
<?php // header.php
....
?>
What you can't see here is that there is a blank - either a space or CR/LF after the closing '?>'. This is output because the php standard says that anything outside the php tags is output as html.
The solution is to make sure that you make sure to erase everything after the closing '?>'

Categories