8

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?

2
  • try to use output buffering for header() to function even header is already sent.. Commented Apr 12, 2013 at 6:33
  • 1
    Use header logically, code header related function in the beginning. I do not find a reason to use then after any output sent. Use mvc pattern. Commented Apr 12, 2013 at 6:38

5 Answers 5

23

You could use

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

to redirect after the headers are sent.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried this... but now the javascript validation doesn't take place?
Well... then i must use only server side validation huh? Or is there a better way?
2

If you using javascript Use this

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

Comments

2

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';

Comments

1

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

Comments

1

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;
?>

1 Comment

Can you elaborate? Why is it more secure?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.