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
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
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.
<?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):
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):
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
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.