Page redirect problem in PHP

Discussion in 'PHP' started by globalcashsite, May 18, 2009.

  1. #1
    Guys

    I am having problem in redirecting to home page of site after registration of member at signup page.

    Once visitor fill registration form and click on submit script add that user to database successfully and also send email to member about registration notification but after that I have this code that is not working:

    header("Location: http://www.yahoo.com");

    This is not working, I don't know what happened. Same script/code was working earlier. Please help me how to redirect users to another page of successful signup.

    Thank you

    GCS
     
    globalcashsite, May 18, 2009 IP
  2. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #2
    Any error messages generated? Any output to the screen before you send this? You could do what vb does and show a screen saying wait a few seconds and then use a meta refresh instead.
     
    exodus, May 18, 2009 IP
  3. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have used same code many times but never error occur, here is error

    Warning: Cannot modify header information - headers already sent by (output started at /home/xxxxxxx/public_html/testtable.php:8)

    I also tried to redirect by two files.

    call.php

    testtable.php

    Now when I run testtable.php in browser it gives above error. Earlier I was redirecting in same testtable.php (call.php code) but later I think I should try redirecting by separate file but that also fails.

    Any help please "After adding member details in database how to redirect visitor to other page like confirmation page etc"? Before redirection a proper php code is executed that adds details in database and send email to user. Up to this (sending email to member) script is working fine and very next line is redirection line that is not working.

    GCS
     
    globalcashsite, May 18, 2009 IP
  4. aaron d.

    aaron d. Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can't use the header() after you output normal content. Just move the include above the HTML.
     
    aaron d., May 18, 2009 IP
  5. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Is there any kind of hosting problem. As I said earlier I used this code many times and did not face such kind of problem. But unusual thing is this time I used CuteFTP to edit and upload files. Instead of editing php files in 1st page or any other editor I open file from my local drive and upload instantly. That is a little bit easy.

    GCS
     
    globalcashsite, May 18, 2009 IP
  6. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #6
    <?php
    require "call.php";
    ?>
    <table>
    <tr>
    <td>
    This is a test line.
    </td>
    </tr>
    </table>
    Code (markup):
    By outputting HTML BEFORE you call up the header function, makes the browser think he is about to read a HTML file. Like aaron said.
     
    Sky AK47, May 18, 2009 IP
  7. aaron d.

    aaron d. Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The way it is working now is the correct way. It is possible your prior server was using some sort of output buffering and compression which delayed the output and gave time for the headers to be sent first, but it is a better habit for headers to /always/ come before content.
     
    aaron d., May 18, 2009 IP
  8. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #8
    Yeah, you need to do the redirect before you output anything to the screen.
     
    exodus, May 18, 2009 IP
  9. octalsystems

    octalsystems Well-Known Member

    Messages:
    352
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    135
    Digital Goods:
    1
    #9
    u should put

    ob_start as a frst statement in ur page
    and after issuing location() function u should use exit()

    let me know if it works
     
    octalsystems, May 18, 2009 IP
  10. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Same code was working on different host. I am also finding another host to experiment same code there and it should work.

    Octalsystem, I will try you method and will update if that works. :)

    GCS
     
    globalcashsite, May 18, 2009 IP
  11. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #11
    you already sent information , headers, tables etc .. you can only redirect header: location when no other stuff has been displayed to the webpage .. once it starts writing the page. nuffin.
     
    ezprint2008, May 19, 2009 IP
  12. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #12
    Bullshit. Use ob_start - either in the php-script itself, or set the environment variable in php.ini. Lets you use headers whenever you want to, regardless of what you've put out of HTML earlier. Might be a performance issue, of course, but that's irrelevant to the fact that it is completely possible to do.
     
    PoPSiCLe, May 19, 2009 IP
  13. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Guys

    Thank you for all helping comments.

    I agree what you people are saying but the most important thing is I run this script very well on other host without any change. So I feel there is some thing configuration matter that is hosting provider dependent. I am finding alternate host so I may check this. :)

    GCS
     
    globalcashsite, May 19, 2009 IP
  14. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #14
    Sounds like your last host had output buffering turned on in the php.ini . It wouldn't output anything until the entire script runs. As a experienced php programmer, I know that if you output anything before the header location redirect then it will throw an error. You can do the ob_start or turn on output buffering in the php.ini file and not throw out this error. Send a message to your web host to turn this on for you or help you setup the php.ini in the root folder for your website.
     
    exodus, May 19, 2009 IP
  15. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Thanks for your helping post. I already have php.ini. Can you tell me command line for php.ini for on_start turning on? :)

    GCS
     
    globalcashsite, May 19, 2009 IP
  16. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #16
    Search you php.ini for "output_buffering" and set it to "on" or a value, like this: output_buffering = 4096

    You might have to uncomment the line, if it's not activated already.
     
    PoPSiCLe, May 20, 2009 IP
  17. iyokz

    iyokz Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    in the beginning of the file just add this ..
    <?php
    ob_start();
    ?>
    bla bla bla ...
    <?
    header("Location: http://www.yahoo.com");
    ?>
    bla bla bla
    <?php
    ob_end_flush();
    ?>
     
    iyokz, May 22, 2009 IP
  18. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Guys

    Today I moved to new host and this problem is solved automatically without any change in script. :)

    Thank you for all people who replied and helped me to understand this problem.


    GCS
     
    globalcashsite, May 28, 2009 IP
  19. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #19
    A suggestion for the future: when it comes down to redirects use Javascript and/or .htaccess redirects. They are much better than PHP redirects.

    Regards,
    Dennis M.
     
    Dennis M., May 28, 2009 IP