简体   繁体   English

单击浏览器后退按钮时调用php函数

[英]call php function when browser back button clicked

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 . 单击“ Browser Back buttonright click然后单击“ Browser Back button项目,然后在用户仍logged-in时阻止页面重定向到login page时,如何调用PHP Function

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. 我发现了这一点(Pressing back button redirects the logged user to log out?)以防止当用户单击browser back button并且用户仍在登录时页面重定向到其他页面,但是我不知道将这些代码放置在何处以及如何放置调用该函数。

Can someone explain this thoroughly to accomplish my problem or provide some DEMO (JSFiddle) for better understanding? 有人可以彻底解释一下以解决我的问题,还是提供一些DEMO (JSFiddle)以便更好地理解?

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. 这样做是什么,它将使用JS检查是否按下了后退按钮,如果按下了后退按钮,则可以执行所需的任何操作。 If a redirect is what you need, use window.location = "http://www.yoururl.com"; 如果需要重定向,请使用window.location = "http://www.yoururl.com"; OR window.navigate("http://www.yoururl.com"); //works only with IE window.navigate("http://www.yoururl.com"); //works only with IE 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: 或者,您可以在此处放置一个jquery ajax调用,该调用将发布的请求发送回特定页面,并且您可以像下面那样检查它们:

if(isset($_POST['ajax_response_from_js_script'])  {
    //call PHP function
}

Hope it helps! 希望能帮助到你!
Keep on coding! 继续编码!
Ares. 战神

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM