views:

52

answers:

4

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
It looks like I'm the only one still using the `href` property!
Daniel Vassallo
Looks like you are the only one doing it properly.
Andrew Moore
@voyager: Thanks for the edit. I didn't notice the OP hinted at a relative path.
Daniel Vassallo
@Daniel: you are welcome.
voyager
A: 
<script type="text/javascript">
window.location.href = "http://www.yoursite.com/index.php"
</script>
Bill Paetzke
A: 

Use window.location

Valentin Rocher
+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.

PHP Documentation: header()
PHP Documentation: ob_start()

Andrew Moore