简体   繁体   English

PHP wordpress cookie重定向

[英]PHP wordpress cookie redirect

Got a problem that I'm having a hard time getting started on. 我遇到了一个很难上手的问题。 Not a big PHP guy so not sure where to start really. 不是一个PHP的大人物,所以不确定从哪里开始。

What I have is this. 我有这个。 I have a web application running on app.xxx.com this. 我有一个在app.xxx.com上运行的Web应用程序。 This is our private application that all our customers use. 这是我们所有客户都使用的私人应用程序。

We are putting together a knowledge base in wordpress of how our software works, help files, etc... this is private information. 我们正在wordpress上建立有关我们软件如何工作,帮助文件等的知识库。这是私人信息。 For business reasons this wordpress runs on kb.xxx.com and must be there. 出于商业原因,此wordpress可以在kb.xxx.com上运行,并且必须存在。

What I am doing currently is when a user authenticates at app.xxx.com I am setting a domain level cookie with some authentication information. 我当前正在做的是当用户在app.xxx.com上进行身份验证时,我正在使用一些身份验证信息设置域级别的Cookie。 This works great. 这很好。

What I need to have happen is on the wordpress side of things if a user browses to kb.xxx.com and does NOT have our parent domain cookie, they are sent to a security error url somewhere. 如果用户浏览到kb.xxx.com并且没有我们的父域cookie,那么我需要做的是在wordpress方面,它们会被发送到某个地方的安全错误网址。 This has to happen server side, can't do this in JS because its not secure enough. 这必须在服务器端进行,由于其不够安全,因此无法在JS中执行。

What files would I edit in the wordpress install to do this kind of check for every request? 我将在wordpress安装中编辑哪些文件以对每个请求进行这种检查? Also how do I check for a parent level cookie in PHP. 另外,我如何检查PHP中的父级Cookie。

Thanks in advance! 提前致谢!

Try this WordPress Action 试试这个WordPress动作

You can try this method. 您可以尝试此方法。 Just add the name of your cookie and add this code to your Functions.php file, located at the root of your theme file. 只需添加Cookie的名称,然后将此代码添加到位于主题文件根目录的Functions.php文件中即可。 If is not there you can just create one. 如果不存在,您可以创建一个。

Paste code into your Functions.php file 将代码粘贴到Functions.php文件中

function has_auth_cookie(){ 
    // See if cookie is set
    if(isset($_COOKIE['cookie_name'])){
        // Do nothing 
    }else{
        // Do Something else
        header('Location: http://www.example.com/');
    }
}
add_action('template_redirect', 'has_auth_cookie');

Conclusion 结论

WordPress has a great Action and Filter system which makes adding code easy as cake. WordPress具有出色的操作和筛选器系统,可轻松添加代码。 By using the filters and actions you can keep your code nice and clean instead of throwing PHP all over your template files. 通过使用过滤器和操作,您可以保持代码的优美和整洁,而不必在整个模板文件中抛出PHP。 This method will survive WordPress core updates as well. 此方法还将在WordPress核心更新中保留下来。 Which is the only way to go if your using WordPress. 如果您使用WordPress,这是唯一的方法。

Here's the list of available actions for WordPress you can try instead of 'template_redirect': http://codex.wordpress.org/Plugin_API/Action_Reference 这是您可以尝试使用的WordPress可用操作的列表,而不是“ template_redirect”: http : //codex.wordpress.org/Plugin_API/Action_Reference

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

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