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?
For example, in PHP, the header () function is used to redirect by specifying the location URL. On the other hand, we can perform page redirects with some client-side script like Javascript. We can prefer to use Javascript redirect when we need to perform navigation on an event basis.
JavaScript is mainly client-side scripting language. Redirect operation can be done with JavaScript on the client-side by providing a new URL to the windows object’s location property like below. Even we can make a Javascript function to use it multiple times easily
We will use the header function of PHP to provide a new URL to the client browser. Keep in mind that after using header function there should be no other operation that will output something or simple be sure header is the last function.
This is an inbuilt PHP function that is used for sending a raw HTTP header towards the client. The syntax of the header () function is as follows: Also, it is likely to apply this function for sending a new HTTP header, but one should send it to the browser prior to any text or HTML.
You could use
<script>
window.location = 'http://www.example.com/newlocation';
</script>
to redirect after the headers are sent.
If you using javascript Use this
window.location.href = "index.php";
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With