PHP Redirect, any good methods?

Discussion in 'PHP' started by 05rmerce, Nov 4, 2007.

  1. #1
    Hey everyone,

    I am looking for what is, in your opinion, the best way of redirecting someone's browser within a PHP script. It's worth mentioning that the header location method won't work for me as I have already sent information ;)

    Thanks in advance,
    05rmerce
     
    05rmerce, Nov 4, 2007 IP
  2. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are only 2 ways of redirecting a page.

    - HTTP Location redirect
    - Meta refresh

    Apologies but Im not quite sure what you mean when you say you have already sent information?

    Could you give us a little more info :)
     
    tonybogs, Nov 4, 2007 IP
  3. 05rmerce

    05rmerce Guest

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well, I mean that the header method:

    <?php
    header( 'Location: http://www.yoursite.com/new_page.html' ) ;
    ?>
    PHP:
    Won't work for me as I need to redirect at the end of a script. If I used that method to do it I would get a php Notice thing telling me that the headers have already been sent.

    Would you mind elaborating on the methods you have given because I am rather new at this.
     
    05rmerce, Nov 4, 2007 IP
  4. Meth_

    Meth_ Well-Known Member

    Messages:
    1,063
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    140
    #4
    
    <?php
    header('location: URL');
    exit;
    ?>
    PHP:


    edit
    oh, I didn't know the header location thing didn't work
    well, I guess this would work


    <script language="javascript">
    document.location = "url";
    </script>
    Code (markup):
     
    Meth_, Nov 4, 2007 IP
  5. 05rmerce

    05rmerce Guest

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Please see my above post; that is the one that will not work for me.
     
    05rmerce, Nov 4, 2007 IP
  6. Meth_

    Meth_ Well-Known Member

    Messages:
    1,063
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    140
    #6
    yeah I know
     
    Meth_, Nov 4, 2007 IP
  7. e96

    e96 Active Member

    Messages:
    299
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    80
    #7
    you only get the header error if you try to print something prior to using the header. you can have php do a bunch of processesing and then redirect at the end of the script. what you can't do is trying printing anything and then call a header. therefore, just make sure that you are not printing anything prior to redirecting and if you have to print something then do what someone else suggested, print out a javascript redirect such as :

    
    <script type="text/javascript"> window.location.href='http://www. webpagetogoto .com/';
    </script>
    
    
    Code (markup):
     
    e96, Nov 4, 2007 IP
  8. AdnanAhsan

    AdnanAhsan Well-Known Member

    Messages:
    601
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #8
    hey may i help you?

    <?php
    ob_start();
    header('location : url');
    ob_flush();
    ?>

    try this..
     
    AdnanAhsan, Nov 4, 2007 IP
  9. James12513

    James12513 Well-Known Member

    Messages:
    1,149
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    138
    #9
    Usualy if it was something like a login script and you wanted to refresh the page into a new one, i would do something like this:

    exit("Logging In
    <meta http-equiv='Refresh' content='1;url=http://www.YOURWEBSITE.com/'>");


    The code; <meta http-equiv='Refresh' content='1;url=http://www.YOURWEBSITE.com/'> is the refresh code
     
    James12513, Nov 4, 2007 IP
  10. theOtherOne

    theOtherOne Well-Known Member

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #10
    You could also put a ob_start() at the very top of your PHP script, before any other output is sent to the browser.
    Output will then be stored in memory until the script has finished executing.
    At any point in the script you can now write
    
    ob_clean();
    header('Location: ...');
    
    PHP:
    and it will work, regardless of any output before these statements.
     
    theOtherOne, Nov 4, 2007 IP