Why won't my redirect work?

Discussion in 'PHP' started by Imozeb, Apr 25, 2010.

  1. #1
    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.
     
    Imozeb, Apr 25, 2010 IP
  2. Brad33

    Brad33 Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Lets see all your code so we can diagnose the problem.
     
    Brad33, Apr 25, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    Imozeb, Apr 25, 2010 IP
  4. Brad33

    Brad33 Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You must be outputting text somewhere above where your redirect code is. Thats what would cause the error.
     
    Brad33, Apr 25, 2010 IP
  5. maestria

    maestria Well-Known Member

    Messages:
    705
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #5
    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.
     
    maestria, Apr 26, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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? :confused:
     
    Imozeb, Apr 26, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    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.
     
    danx10, Apr 26, 2010 IP
  8. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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?
     
    Imozeb, Apr 26, 2010 IP
  9. nezZario

    nezZario Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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
     
    nezZario, Apr 27, 2010 IP
  10. nezZario

    nezZario Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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!!!
     
    nezZario, Apr 27, 2010 IP
  11. joyhitech1980

    joyhitech1980 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    test this code it well for me
    
    $URL="hxxp://www.yyy.com/index.php";  
    header ("Location: $URL");  
    
    Code (markup):
     
    joyhitech1980, Apr 27, 2010 IP
  12. majin22

    majin22 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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; ?>
     
    majin22, Apr 27, 2010 IP
  13. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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.
     
    Imozeb, Apr 28, 2010 IP
  14. fundl

    fundl Member

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #14
    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.
     
    fundl, Apr 29, 2010 IP
  15. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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!
     
    Imozeb, Apr 29, 2010 IP
  16. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #16
    RewriteRule ^$ /php/
     
    gapz101, Apr 29, 2010 IP
  17. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #17
    RewriteRule ^$ /php/ did not work. Any other ideas?
     
    Imozeb, Apr 29, 2010 IP
  18. rilwis

    rilwis Peon

    Messages:
    104
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Try to use this:

    RewriteRule ^(.*)$ /php/$1 [NC, L]
     
    rilwis, May 1, 2010 IP
  19. ched

    ched Active Member

    Messages:
    185
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #19
    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.
     
    ched, May 1, 2010 IP
  20. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Got it! Thanks everyone!
     
    Imozeb, May 1, 2010 IP