Okay, I have the following php file:
add a header()
function to redirect your page after download success
header( "refresh:5;url=yourlocation.php" );
Resource link(here)
Use html or javascript redirects, because it works even if headers were already sent. Just echo this if the download is a sample file.
e.g.
HTML
echo "<meta http-equiv = 'Refresh' content = '2; url =./redirectpage.php'/>"; // change redirectpage.php to where you want to redirect.
JavaScript
echo '<script type="text/javascript">
window.location.href="./redirectpage.php";
</script>';
Best bet is to turn your page order around a little bit. Set up a page that's a "thank you for downloading this sample" page, and have it set up to do a javascript refresh that actually takes you to the download. As it's a download, not a new page, the thank you page will still be there. In case they don't have javascript on the browser, put a link to the actual file.
You could have a page for each file, but best bet would be to pass the filename in as a get var, i.e. ThankYou.php?file=sample.pdf
This code worked in my situation well. Maybe it will help you. It is main page:
<form action="/download.php" method="post">
<button type="submit">Download</button>
</form>
This script in main page:
$(document).on('submit', 'form', function() {
setTimeout(function() {
window.location = "/thanks.html";
}, 1000);
});
You must write your php code for download in dowload.php. If in download.php your code only downloads file, then your main page will not be reloaded. So the script which handles form will work.