This question already has an answer here:
- How to make a redirect in PHP? 28 answers
Can PHP make a redirect call after executing a function? I am creating a function on the completion of which I want it to redirect to a file located in the same root folder. Can it be done?
if {
//i am using echo here
}
else if ($_SESSION['qnum'] > 10) {
session_destroy();
echo "Some error occured.";
//redirect to user.php
}
Using a javascript as a failsafe will ensure the user is redirected (even if the headers have already been sent). Here you go:
If you need to properly handle relative paths, I've written a function for that (but that's outside the scope of the question).
this works for me fine.
if you want to include the redirect in your php file without necessarily having it at the top, you can activate output buffering at the top, then call redirect from anywhere within the page. Example;
actually, I found this in the code of a php based cms.
redirect('?module=blog', 0);
so it is possible. In this case, you are logged in at the admin level, so no harm no foul (I suppose). the first part is the url, and the second? I can't find any documentation for what the integer is for, but I guess it's either time, or data since it is attached to a form.
I, too, wanted to refresh a page after an event, and placing this in a better spot worked out well.
But you can't first do an echo, and then redirect.