I’m writing a PHP script and would like to present a warning message if the user clicks the back button on the browser. Is this something I can do in PHP? The only solution I’ve been able to find is via javascript, however it presents a warning message every time something is submitted on the page. If there is not a way to do this in PHP, can this javascript be altered to only work when the back button is pressed on the browser? Thanks.
<script type="text/javascript" language="javascript" defer="defer">
window.onbeforeunload = eventTermination;
function eventTermination(args) {
args.returnValue = ('WARNING:CANNOT GO BACK');
}
</script>
You can’t use PHP for this as PHP is a server-side scripting, means that the script is compiled on the server and sent to the client broswer as a HTML file.
But JS is a client-side script, which means that it is compiled on browser side (client side).
So for your request, PHP is not possible.