rorybing Posted February 3, 2012 Share Posted February 3, 2012 Hi, I'm trying to figure out if I can get a redirect page to work, without needing to use the header("Location: page.php"); command. I know with asp, there is a response.redirect for redirecting pages, but, with the header, I can only use this, if there is no output before calling the command. Is there any method that can be used where I can process some code, if output is required, and depending on processing on the page, redirect it to another page, using a command similar to response.redirect? I have seen the http_redirect, but I can only see this working with clearing buffers etc. I have also a redirect_to("page.php"); command, but I cannot get this to work. I am using php 5. Any help would be appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/256333-php-redirect/ Share on other sites More sharing options...
spiderwell Posted February 3, 2012 Share Posted February 3, 2012 you could get the php to output some javascript that would do the redirect, or a meta refresh tag Quote Link to comment https://forums.phpfreaks.com/topic/256333-php-redirect/#findComment-1314055 Share on other sites More sharing options...
rorybing Posted February 3, 2012 Author Share Posted February 3, 2012 Hmmmm, might need to start learning JavaScript in that case. Not the most experienced programmer here to be honest Quote Link to comment https://forums.phpfreaks.com/topic/256333-php-redirect/#findComment-1314058 Share on other sites More sharing options...
AyKay47 Posted February 3, 2012 Share Posted February 3, 2012 sending an HTTP header via header() is normally the preferred solution, as that is what they are there for, perhaps if we saw the logic in your code we could provide a more detailed answer. Quote Link to comment https://forums.phpfreaks.com/topic/256333-php-redirect/#findComment-1314061 Share on other sites More sharing options...
ManiacDan Posted February 3, 2012 Share Posted February 3, 2012 asp uses output buffering to get around the problem with header redirects, the asp redirect is the same command. You can use ob_start to start output buffering, make your decision, and if you need to redirect, kill the buffer and send the header. Quote Link to comment https://forums.phpfreaks.com/topic/256333-php-redirect/#findComment-1314063 Share on other sites More sharing options...
rorybing Posted February 3, 2012 Author Share Posted February 3, 2012 Hi ManiacDan, Thanks for the reply. What would be the command for stopping the buffer? Where would the buffer start? Before the DOCTYPE at the top of the page, or before any code has been written for displaying the page? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/256333-php-redirect/#findComment-1314065 Share on other sites More sharing options...
Adam Posted February 3, 2012 Share Posted February 3, 2012 Personally I would say there's no need to use output buffering. Just ensure all your "business logic" is executed before you begin to output the response body and you will be fine. Quote Link to comment https://forums.phpfreaks.com/topic/256333-php-redirect/#findComment-1314068 Share on other sites More sharing options...
rorybing Posted February 3, 2012 Author Share Posted February 3, 2012 Hi, Thanks for all the replies. I ended up just making a function, passing the page I wanted into that function, and redirecting from there. Did the trick. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/256333-php-redirect/#findComment-1314089 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.