I'm writing a code to redirect to home-page after logging out. I used to use
header("Location: index.php");
but I found in some tutorial that I can use
echo "<script>location.href = 'index.php'</script>";
Now I want to know the difference between both scripts and which is better?
CodePudding user response:
The first one uses PHP, and the second uses Javascript (you'd need to make sure it was echo'ed inside <script></script>
tags).
The Javascript solution requires Javascript to be enables and makes the browser do a little more work than the PHP header method. I think the PHP method, header("Location: index.php");
is safer. You would just need to make sure to use die();
after the header command or else risk loading the rest of the page before the redirect can be processed.
Another consideration is that the pure php solution won't work if you've already echo'ed anything (including whitespace). So in some situations, echo "<script>location.href = 'index.php'</script>";
could be a better choice.
CodePudding user response:
Like Stevish mentioned, the second one is Javascript and could be deactivated by the user, if he deactivates Javascript. With PHP the Server will do the redirect.