How can I call a PHP Function when Browser Back button clicked or right click then clicked BACK item then prevent page to redirect to login page when user is still logged-in.
I found this (Pressing back button redirects the logged user to log out?) to prevent page to redirect to other page when user clicked browser back button and user is still logged in, but I didn't know where to put those codes and how to call that function.
Can someone explain this thoroughly to accomplish my problem or provide some DEMO(JSFiddle) for better understanding?
You could use a header redirect on the login page to check if the user is logged in, but this will have to be called before any page data is sent:
if(isset($_SESSION['username'])){
header("Location: home.php");
}
You can try to use this:
jQuery(document).ready(function($) {
if (window.history && window.history.pushState) {
window.history.pushState('forward', null, './#forward');
$(window).on('popstate', function() {
alert('Back button was pressed.'); //here you know that the back button is pressed
});
}
});
You can find the answer Here
What this does, it checks with JS if the back button was pushed and if so, you can perform whatever action you want. If a redirect is what you need, use window.location = "http://www.yoururl.com"; OR window.navigate("http://www.yoururl.com"); //works only with IE
Alternatively, you can place an jquery ajax call there, that sends posted requests back to a certain page, and you can check them like:
if(isset($_POST['ajax_response_from_js_script']) {
//call PHP function
}
Hope it helps!
Keep on coding!
Ares.
Related
This is a part of my payment.php code.
$f=1 when the values got stored in database correctly.
if($f==1){
echo "<script>alert('Rooms Successfully Reserved')</script>";
echo "<script>window.location.replace('home.php')</script>";
}
Now we all know window.location.replace() replaces the current page, removing the previous one from the back button history.
But the back button is working.
I do not want the user to press the back button and enter the payment.php again.
So, what should i do with it?
Same goes for my login.php page, it redirects to home.php but again one can press back button and get to login.php.
How should i prevent it?
I even tried this:
window.history.forward()
But even it isn't working.
Please help
I think you can work with the Unload Event and the History Function.
<script type="text/javascript">
function back() {
window.history.forward();
}
// Force Client to forward to last (current) Page.
setTimeout("back()", 0);
window.onunload = function () { null };
</script>
Add this before your Body Tag gets closed.
My question is almost similar to question question here.
I am have a standard sidebar homepage, which executes ajax to replace text inside a div tag and show context. All works fine except the back button. The back button redirects to login page.
These are the things I tried : (I am using servlets)
Without any javascript, user is sent to login.jsp on back button, (session is not invalidated).
I have written code in login.jsp to redirect to homepage when user is already logged in.
access = Integer.parseInt(session.getAttribute("access").toString());
if (access == 1) {
RequestDispatcher rd = request.getRequestDispatcher("ajaxContent?url=homepage");
rd.forward(request, response);
//ajaxContent is the servlet loads the sidebar. With the content of homepage in div tag.
}
But back button just displays the previous page, so the java code to check session is never executed.
I tried to add a onbeforeunload function to alert the user on back button.
window.onbeforeunload = function () {
return "Your work will be lost.";
};
But it also displays the alert when saving forms saying "Your data will not be saved", well, that's not a very good way to accept forms, right?
Lastly I tried history.pushState function to replace the history when ever the ajax function is called.
window.history.pushState({}, 'Previous','www.example.com/ajaxContent?url=homepage');
But that just replaces the url in the browser but doesn't load the page. On multiple clicks, it displays login.jsp again. (obviously)
I also found this page which has source code to disable the back button entirely, but I would prefer to make the back button the replace the div tag with the previous content that was in there.
I want to refresh the current page when clicking browser back button, it refreshes the same page but the page is refreshed even when I click logout button in the same page. Please give me valuable solution. Thanks in advance
window.addEventListener('popstate', function (event) {
if (window.event) {
location.reload(true);
}
}
Friend you can not play with the browser function to go back. But you can add in the function of close session the following code
location.reload();
You can also place an event that triggers the same function that will reload the page
And if you want to redirect to another url you can use the following code
window.location.replace("http//:URL.COM");
I have an anchor on my page which looks like this: <a id="login" href="login.php"></a>
However, when a user inputs data in the page, in order that he shouldn't lose that data when going to the login page (the data can be saved without being logged in), I change it by taking out the href and adding an onclick to warn the user, as follows:
if (-code which checks for user input-){
$('#login').attr('href','javascript:void(0)');$('#login').attr('onclick','logincheck()');
}
function logincheck(){
alert("Going to the login page will make you lose your work. If you want to save them to a collection, please do so now. When you're ready, click the login button again.");
$('#login').attr('onclick','');$('#login').attr('href','login.php');
}
So the user gets the warning, and now the he can click the login button again to login.
The problem I'm having that for some reason the $('#login').attr('href','login.php');makes the page redirect right away. I'm guessing this is because the we're still in the middle of the anchor click.
How can I change this href but keep the page from actually redirecting before the button is clicked again? I tried adding return false but that didn't help.
Thanks in advance!
Why not consolidate it into a single item?
$('#login').on('click',function(e){
if($(this).data('clicked')!=='true'){
e.preventDefault();
alert("Going to the login page will make you lose your work. If you want to save them to a collection, please do so now. When you're ready, click the login button again.");
$(this).data('clicked','true');
}
});
This will prevent the action the first time, provide the alert, and give it the clicked data to show it has been clicked. The second time it won't meet the if condition, and continue to login.
This avoids the javascript:void(0) bit, as well as any onclick attributes ... everything is contained in your JS file.
You need e.preventDefault();
$('#login').on('click', function(e){
if(!$(this).data('clicked')){
$(this).data('clicked', true);
e.preventDefault();
alert("Going to the login page will make you lose your work. If you want to save them to a collection, please do so now. When you're ready, click the login button again.");
}
});
I am trying to handle browser back button event but i could not find any solution.
I want to ask user if he clicks on browser back button using "confirm box" if he chooses ok i have to allow back button action else i have to stop back button action.
Can any one help me in implementing this.
Warn/confirm User if Back button is Pressed is as below.
window.onbeforeunload = function() { return "Your work will be lost."; };
You can get more information using below mentioned links.
Disable Back Button in Browser using JavaScript
I hope this will help to you.
You can also add hash when page is loading:
location.hash = "noBack";
Then just handle location hash change to add another hash:
$(window).on('hashchange', function() {
location.hash = "noBack";
});
That makes hash always present and back button tries to remove hash at first. Hash is then added again by "hashchange" handler - so page would never actually can be changed to previous one.