I have the Index file like this
<?php
include('file1.php');
include('file2.php');
?>
in file1.php I am having the code like below.
<?php
echo "hai";
?>
I am trying to redirect the page by using header('Location:index.php').It throws an error something like the output already started.I know for header if we give the echo statement it throws an error.In this situation I am trying to redirect by using Javascript window.location.href='index.php' .It gives me the expected output and there is no error.Why?.
The PHP header command is sending an instruction to the response stream that the browser will interpret as a redirect to a new location. But, by its nature, you can only send a header of any kind before you has started sending content. In your example, you send the content “hai” then tried to send the header.
Now your javascript is executed in the broswer by the javascript engine. As such, the window.location is telling the javascript engine to make the browser request a new page. This is independent of any information contained within the http stream (in theory, the location could be something other than http such as ftp or a mailto).