[英]Brower back button issue in wordpress after logout
I am using the below redirect code in the functions.php
file.我在
functions.php
文件中使用以下重定向代码。
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
$adfsurllogouturl = 'some url';
wp_redirect( $adfsurllogouturl );exit();
}
It's working fine.它工作正常。 The issue is that once logged out, it redirects to
SOMEURL
when I click on the back button of the browser from the redirected page, it shows previous page details.问题是,一旦注销,当我从重定向页面单击浏览器的后退按钮时,它会重定向到
SOMEURL
,它会显示上一页的详细信息。 But I want that it should go on the login page.但我希望它应该在登录页面上 go 。
I used the below code to fix it but it's not working.我使用下面的代码来修复它,但它不起作用。
function check_if_user_is_loggedin_action()
{
if ( is_user_logged_in() )
{
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
}
add_action('init', 'check_if_user_is_loggedin_action');
Please suggest to me.请给我建议。 Is it the correct way?
这是正确的方法吗?
To redirect users after successfull logout to the needed page, you can add the following code:要在成功注销后将用户重定向到所需的页面,可以添加以下代码:
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
wp_safe_redirect( '/login-page' );
exit();
}
With the following button code:使用以下按钮代码:
<a href="<?php echo wp_logout_url(); ?>" title="Logout">Logout</a>
Instead of /login-page
please, use the needed URL.请使用所需的 URL 而不是
/login-page
。
Upd.更新。
According to the solution provided here: https://stackoverflow.com/a/40766644/17572092根据此处提供的解决方案: https://stackoverflow.com/a/40766644/17572092
You can use below code to check for session variable:您可以使用以下代码检查 session 变量:
<?php
if(!isset($_SESSION['login'])) :
header("Location: login.php");
?>
When user logout destroy the session:当用户注销时销毁 session:
<?php
unset($_SESSION['login']);
session_destroy();
?>
<pre><code>
add_action(‘wp_logout’,’auto_redirect_after_logout’);
function auto_redirect_after_logout(){
wp_redirect( home_url() );
exit();
}
add_action( ‘check_admin_referer’, function($action, $result) {
if ( ‘log-out’ == $action && is_user_logged_in() && !empty($_GET[‘action’])
&& ‘logout’ == $_GET[‘action’] ) {
wp_logout();
wp_redirect( home_url() );
exit();
}}
</code></pre>
I have resolved my problem as follows.我已经解决了我的问题如下。
I added a cookie in the functions.php
file just after logging in.登录后,我在
functions.php
文件中添加了一个 cookie。
function login_function() {
setcookie('wp_user_logged_in', 1, time() + 31556926, '/');
$_COOKIE['wp_user_logged_in'] = 1;
}
add_action('wp_login', 'login_function');
I deleted and set the cookie value to NULL in the functions.php
file just after logout.注销后,我在
functions.php
文件中删除了 cookie 值并将其设置为 NULL。
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
unset($_COOKIE['wp_user_logged_in']);
setcookie('wp_user_logged_in', null, -1, '/');
wp_safe_redirect( '/login-page' );
exit();
}
Added following javascript in footer.php
file to check the user is logout and reload the page.在
footer.php
文件中添加以下 javascript 以检查用户是否注销并重新加载页面。
<script type="text/javascript">
setInterval(function(){
if (document.cookie.indexOf('wp_user_logged_in') !== -1) {
//do something when user logged in
} else {
//do something when user logged out
window.location.reload();//reload page
}
},2000);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.