Products
GG网络技术分享 2025-03-18 16:12 52
I am using Wordpress as an application that requires all users to be logged in to access. The function that I have been using to do this is
function logoutApp_redirect() {global $post;
if( ! is_user_logged_in() ){
wp_redirect( home_url() . \'/dashboard\' );
}
}
add_action( \'template_redirect\', \'logoutApp_redirect\' );
if a user is logged out then I want to return them to the page that they were previously on. For example, if a user is on home_url() . \'/page1\' ); and they are logged out for inactivity return them to this page upon login.
A few ideas I had was first to use AJAX and overlay the login (so they never leave the page they are on) and upon login the overlay is removed. This is my ultimate solution, yet for security, anyone with the knowledge of inspect could hide the overlay to see the private information.
Thus the reasoning for adding a redirect to the wp-login and bringing my second idea of when a user is logged out they are redirected and upon login they are returned to the previous page. This is the better method for security but it means that any information that was not stored to the DB will be lost.
How can I address a solution that will fix these issues?
PLEASE NOTE I do not want a plugin as the solution and would prefer to keep things operating from within my functions.php
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我使用Wordpress作为一个需要所有用户登录才能访问的应用程序。 我一直用来做的函数</ strong>是</ p>
function logoutApp_redirect(){global $ post;
if(!is_user_logged_in() ){
wp_redirect(home_url()。\'/ dashboard\');
}
}
add_action(\'template_redirect\',\'logoutApp_redirect\');
</ code> </ pre>
if </ code>用户注销后我想将它们返回到之前所在的页面。 例如,如果用户在 home_url()上。 \'/ page1\'); </ code>并且他们在登录时注销不活动将它们返回到此页面。</ p>
我的一些想法是第一次使用 AJAX < / strong>并覆盖登录(因此他们永远不会离开他们所在的页面)并在登录时删除叠加层。 这是我的终极解决方案,但为了安全起见,任何具有 inspect </ strong>知识的人都可以隐藏叠加层以查看隐私信息。 </ p>
因此,将重定向添加到 wp-login </ code>并将我的第二个想法添加到用户注销时的原因是重定向的,并且在登录时它们是 返回上一页。 这是更好的安全方法,但这意味着任何未存储到 DB </ strong>的信息都将丢失。</ p>
如何解决将要解决的问题 修复这些问题?</ p>
请注意</ strong>我不希望插件作为解决方案,并且希望在我的 functions.php <中保持操作 / code> </ p>
</ div>
网友观点:
You can use PHP $_SESSION
if( ! is_user_logged_in() ){$_SESSION[\'user_previous_page\'] = $server[\'REQUEST_URI\'];
wp_redirect( home_url() . \'/dashboard\' );
}
Then after the login you can send the user to the previous page
wp_redirect(home_url() . $_SESSION[\'user_previous_page\']);
Demand feedback