anshu
anshu

Reputation: 91

PHP Page redirection with javascript

a small problem ! I have a login form in a PHP file. I have used javascript validation for the form. On a login success scenario i am redirecting the user to their home page.

I have used header("Location:index.php").

I know that the header must be before any output must be sent to the browser. My question is there any walk around to do this redirection?

Upvotes: 8

Views: 83187

Answers (5)

MRE20
MRE20

Reputation: 25

I just wanted to add a quick note to the previous answers. if you want to use JavaScript for redirection generating the script form the server-side is more secure.

<?php
$script = "<script>
window.location = 'http://www.example.com/newlocation';</script>";
echo $script;
?>

Upvotes: 1

Thirumalai murugan
Thirumalai murugan

Reputation: 5896

If you are ready to use the JavaScript you can use any one of the following method.

1.window.location.assign('http://www.example.com');
2.window.location = 'http://www.example.com';
3.window.location.href = 'http://www.example.com';

Upvotes: 2

chandresh_cool
chandresh_cool

Reputation: 11830

If you using javascript Use this

window.location.href = "index.php";

Upvotes: 2

Tom
Tom

Reputation: 4592

You could use

<script>
    window.location = 'http://www.example.com/newlocation';
</script>

to redirect after the headers are sent.

Upvotes: 23

Raptor
Raptor

Reputation: 54212

Apart from using header() to redirect, you can use meta refresh method, or JS window.location method.

Upvotes: 1

Related Questions