This is my redirect. PHP Code: header('Location: http://www.mysite.com/myfolder/myfile.php'); Code (markup): I recently switched to a Linux server and my redirects stopped working. It states 'header already sent' or it just doesn't redirect. Why is this and how do I fix it? Thanks.
This is the PHP code. if(isset($_POST['startphp'])) { //there is more code here but I deleted it and tested it in my browser and it still gives the same error message header('Location: http://www.mysite.com/myfolder/myfile.php'); } Code (markup): This code runs after I submit an html form. (The form action is the same page and javascript sets 'startphp' to true.) Now that I think about it all the header redirects that are not working are called via html form. Could this be the problem? And is there another way to use PHP to redirect a user? Thanks.
You must be outputting text somewhere above where your redirect code is. Thats what would cause the error.
is there any echo or print_r or some similar code before the redirection, if so it wont work, you will have to use some meta redirect, though not highly desired.
Normally this redirect would have some code above it, but I deleted all of the code and placed the redirect at the top of my page. I tested it in my browser and it still gave the error. Does PHP have an alternate way of redirecting users?
Try adding the following to the top of the file (first line?) where the code resides: <?php ob_start(); ?> PHP: Also would help if you included the exact error you see. The only method PHP has for redirection is via header(), however it can also be achieved using html (meta refresh tag) or javascript (window.location), but these options may not be as swift as PHP's.
Here is the error message: <br /> <b>Warning</b>: Cannot modify header information - headers already sent by (output started at script/file_upload.php:6) in <b>/script/file_upload.php</b> on line <b>9</b><br /> Code (markup): I just can't figure out why switching to Linux servers would cause this kind of problem?
The problem was with your previous host, they had output buffering enabled in php.ini .. This is kind of like calling ob_start() at the top of every page and ob_end_flush() at the end (up to 4k of data anyway, It only buffers so much at once) Once again, as others have suggested: Try your hardest to NOT have output BEFORE the header() .. This is the most proper way NO output. Not HTML, not whitespace, NOTHING before the header() call. Rearrange your code. if you still can't get that to work, call ob_start() at the VERY top of the page (BEFORE ANY OUTPUT!!!) .. then call ob_end_flush() at the END of the page. this might cost you some performance, but it will fix the issue
sorry, if you have access to PHP.ini , also see the "output_buffering" variable. you could set output_buffering = 4098 and it will also fix your issue. but you must have access to php.ini on the system (or some equivilent) .. you may also be able to set it in htaccess, i'm not sure try creating a file called ".htaccess" file and adding php_value output_buffering 4098 and uploading to the root directory of your server. make sure it's called ".htaccess" -- not .htaccess.txt or htaccess.txt but ".htaccess" good luck!!!
test this code it well for me $URL="hxxp://www.yyy.com/index.php"; header ("Location: $URL"); Code (markup):
if you don't want to spend a lot of time debugging the php code problem try this: <?php if(isset($_POST['startphp'])): ?> <script type="text/javascript"> document.location.href = "http://www.mysite.com/myfolder/myfile.php" </script> <?php endif; ?>
It's still not working... I tried ob_start(); ob_end_flush(); and I tried to create a php.ini file and upload it to my directory but it isn't working. This is my php.ini files code: session.save_path = "/mnt/w9999/.phpsessions" register_globals = on upload_tmp_dir = "/tmp/" upload_max_filesize = 20000000 post_max_size = 20000000 zend_extension = /usr/local/nf/share/ioncube/ioncube_loader_fre_5.2.so extension_dir = /usr/local/nf/php5/lib/php/extensions extension=pdo_mysql.so output_buffering = 4098 Code (markup): Does it look okay? Also by at the top of the page you mean like this <?PHP header("Location: http://www.yyy.com"); ?> right? Thanks.
have you checked whitespaces in your code because a single whitespace also is like an output for php.And the rule says there should be no output before the headers that's only the solution of your problem i think.
It is fixed. I used the php5.ini file. Also since I seem to be getting no response in the Apache section of this forum mabye someone here could answer my question? I want to redirect users from www.mysite.com to www.mysite.com/php/index.php using a .htaccess file. Does anyone know how to do this? Thanks to everyone for your help!
Thanks for this thread it helped me with an issue relating to redirection and $_POST . You gave me enough clues to be able to resolve a problem I have had for 3 days!!!! Anyway I found a great Firefox addin that may help if you have any future issues with headers. Its called 'Live HTTP headers' basically you start it then open the page you have an issue with. Then you can actually see the header request and header responses. Its great for solving headers problems.