Results 1 to 7 of 7

Thread: [RESOLVED] Browser redirection problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    4

    Resolved [RESOLVED] Browser redirection problem

    Hey peepz
    I can't find any info on this from 't3h g00gl3', and am in quite a rush to get this site finished, so I'll ask here:
    It's come to my attention that you cannot redirect the browser to another site if the browser has already recieved HTML tags.
    Q1, is this true? I guess so, as its not working for me
    Q2: Is there a way around it?

    Be sure that you do not have any text sent to the browser before this, or it will not work. Your safest bet is to simply remove all content from the page but the redirect code.
    PHP Code:
        <html>
        <?php
        
    //this will NOT work, the browser received the HTML tag before the script
        
    header'Location: http://www.yoursite.com/new_page.html' ) ;
        
    ?>
    Source: http://php.about.com/.../phpredirection.htm

    Thanks

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Browser redirection problem

    1) Yes.

    2) Um, call header() and exit() before sending any output?

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Browser redirection problem

    yes, it's true -- you can't send headers to a browser if the headers are already sent. once ANY output is given to the browser, headers have been sent.

    there is a way around it, although it may not be the best way, but you can simply use output buffering. basically, what an output buffer does it hold the data and doesn't send anything to the browser until your you send the buffer to the browser itself. check out ob_start() to get started, on the left-hand side all of the relating buffer functions are there.

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Browser redirection problem

    Quote Originally Posted by blowman
    It's come to my attention that you cannot redirect the browser to another site if the browser has already recieved HTML tags
    So... what do you think you should do? it can't work once they have seen sent you may try putting it before any content is sent
    My Blog.

    Ryan Jones.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Browser redirection problem

    Quote Originally Posted by kows
    there is a way around it, although it may not be the best way, but it does refrain from using javascript or anything like that to redirect instead. you can simply use output buffering -- and then if something unexpected happens (ie: login error, or some script error, or whatever), you can simply delete the buffer's contents and send a header to redirect somewhere instead.
    That's a horrible retarded idea. Just do your logic processing before sending any output. Logic and presentation should always be kept completely separate.

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Browser redirection problem

    oops, I edited my post before seeing anyone had replied.

    either way, it's a work around. I already said it wasn't the best way. I've never had to do it, but it could be incredibly inconvenient to have to rewrite everything to do what you're planning on and I've had plenty of experiences where it would be great to use output buffering. there's other ways around it as well, output buffing is just one way. if you don't like it, don't do it!

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    4

    Re: Browser redirection problem

    call header() and exit() before sending any output?
    it can't work once they have seen sent you may try putting it before any content is sent
    Just do your logic processing before sending any output.
    Ok i have done this and it works, thanks. I should of realized that first, but its been a long day

    Thanks again for all your help people.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width