Hello. I want to redirect someone to index.php, how do i do this? But not the "meta" method, because it needs to be in header, and i can not have it there.
+2
A:
window.location.href = 'http://your-new-url.com';
or
window.location.pathname = 'index.php';
if you need to use a relative pathname.
Daniel Vassallo
2010-02-18 18:13:21
It looks like I'm the only one still using the `href` property!
Daniel Vassallo
2010-02-18 18:16:05
Looks like you are the only one doing it properly.
Andrew Moore
2010-02-18 18:19:17
@voyager: Thanks for the edit. I didn't notice the OP hinted at a relative path.
Daniel Vassallo
2010-02-18 18:28:25
@Daniel: you are welcome.
voyager
2010-02-18 18:31:52
A:
<script type="text/javascript">
window.location.href = "http://www.yoursite.com/index.php"
</script>
Bill Paetzke
2010-02-18 18:13:32
+1
A:
Do not use JavaScript for this. If the user has JavaScript disabled (or most likely is browsing with a browser without support for JavaScript), the redirection will not work.
From your PHP code, you can send an HTTP header to direct your user to the page of your choice. Use the header()
function to do this.
header('Location: index.php');
exit; // Important, stops execution of PHP page
If PHP complains that it cannot send the header because data has already been sent to the browser, simply go to the top of your script and enable output buffering by using ob_start()
:
ob_start();
With output buffering enabled, you can send headers anywhere in your code since data is only sent at the end of your script.
Andrew Moore
2010-02-18 18:22:34