I'm refactoring some old code and trying to understand headers
a bit better.
I read an awesome answer in the following post on No output before sending headers! Now I understand the 'why' but when it comes to implementation Its still a bit fuzzy.
My current page redirects back when the cancel button is clicked with the value searched.
I store the page the request was made from and the value in session variables allowing me to do this:
if (isset($_SESSION['searchPage'])){
header('Location:searchForm.php?ticket='.$_SESSION['product'].'&searchbtn=Search');
exit();
}
However to make this work in the display page I had to to use ob_start()
.
To avoid this workaround I found that I could redirect via javascript:
if (isset($_SESSION['searchPage'])){
echo '<script>window.location="searchForm.php?product='.$_SESSION['product'].'&searchbtn=Search";</script>';
exit();
}
Now to my questions
- Which method is better or acceptable?
- I can't seem to think of a way to design my page in such away where no output is sent before using
header()
. If a button click event causes redirection how do you handle redirection in php without usingob_start()
?