WordPress从插件登录用户时未设置会话cookies

您好, 我正在使用一个自定义插件实现从非WP站点到WP站点的SSO。基本设置似乎工作正常,在验证JWT令牌之后,在WP中创建用户,最后一步是将用户重定向到WP站点。这是我在没有设置cookies的地方遇到的问题代码。 if ($user) {wp_clear_auth_cookie();wp_set_current_user($user->ID, $user->user_login);wp_set_auth_cookie($user->ID);do_action(\"wp_login\", $user->user_login);wp_safe_redirect($redirectUrlAfterLogin);exit();} 我不是WP开发人员,如果这不是合适的论坛,请道歉。感谢您的宝贵见解。 谢谢, 这取决于您的代码是在什么环境下执行的。通常无法设置cookies是因为浏览器的输出已经开始。尝试在输出启动后使用PHP设置cookie将导致记录\"Headers already sent\"错误。检查您的错误日志以查看是否存在这种情况。 是的,我在日志中将此视为一个警告。 [12-Sep-2020 15:04:32 UTC] PHP Stack trace:[12-Sep-2020 15:04:32 UTC] PHP1. {main}() /Users/arul/Local Sites/docs/app/public/index.php:0[12-Sep-2020 15:04:32 UTC] PHP2. require() /Users/arul/Local Sites/docs/app/public/index.php:17[12-Sep-2020 15:04:32 UTC] PHP3. wp() /Users/arul/Local Sites/docs/app/public/wp-blog-header.php:16[12-Sep-2020 15:04:32 UTC] PHP4. WP->main() /Users/arul/Local Sites/docs/app/public/wp-includes/functions.php:1285[12-Sep-2020 15:04:32 UTC] PHP5. WP->parse_request() /Users/arul/Local Sites/docs/app/public/wp-includes/class-wp.php:745[12-Sep-2020 15:04:32 UTC] PHP6. do_action_ref_array() /Users/arul/Local Sites/docs/app/public/wp-includes/class-wp.php:388[12-Sep-2020 15:04:32 UTC] PHP7. WP_Hook->do_action() /Users/arul/Local Sites/docs/app/public/wp-includes/plugin.php:544[12-Sep-2020 15:04:32 UTC] PHP8. WP_Hook->apply_filters() /Users/arul/Local Sites/docs/app/public/wp-includes/class-wp-hook.php:311[12-Sep-2020 15:04:32 UTC] PHP9. rest_api_loaded() /Users/arul/Local Sites/docs/app/public/wp-includes/class-wp-hook.php:287[12-Sep-2020 15:04:32 UTC] PHP10. WP_REST_Server->serve_request() /Users/arul/Local Sites/docs/app/public/wp-includes/rest-api.php:339[12-Sep-2020 15:04:32 UTC] PHP11. WP_REST_Server->dispatch() /Users/arul/Local Sites/docs/app/public/wp-includes/rest-api/class-wp-rest-server.php:376[12-Sep-2020 15:04:32 UTC] PHP12. ja_login() /Users/arul/Local Sites/docs/app/public/wp-includes/rest-api/class-wp-rest-server.php:1050[12-Sep-2020 15:04:32 UTC] PHP13. getUserFromValidToken() /Users/arul/Local Sites/docs/app/public/wp-content/plugins/jwt-authenticator/auth.php:54[12-Sep-2020 15:04:32 UTC] PHP14. wp_clear_auth_cookie() /Users/arul/Local Sites/docs/app/public/wp-content/plugins/jwt-authenticator/auth.php:86[12-Sep-2020 15:04:32 UTC] PHP15. setcookie() /Users/arul/Local Sites/docs/app/public/wp-includes/pluggable.php:988[12-Sep-2020 15:04:32 UTC] PHP Warning:Cannot modify header information - headers already sent by (output started at /Users/arul/Local Sites/docs/app/public/wp-content/plugins/jwt-authenticator/JWT.php:36) in /Users/arul/Local Sites/docs/app/public/wp-includes/pluggable.php on line 989 然后您需要在WP初始化过程中更早地设置auth cookie,或者在稍后以某种方式阻止输出。如果它是您的代码生成输出,那么您可以对此做些什么。如果输出来自其他代码,则只能提前设置cookie。 当然,只有知道当前用户后,才能设置auth cookieinit\"是确定用户后最早的操作挂钩。 好的,谢谢。这完全有道理。 我有一个restu apiu init钩子,它处理整个身份验证过程,并将经过身份验证的用户重定向到wordpress。 // register the callbackadd_action(\"rest_api_init\", function () {register_rest_route(\"jwt-auth/v1\", = = = = = $jwt[\"nbf\"] ?? == $iss) && ($jwt[\"aud\"] == $aud) && ($exp > $now) && ($now > $nbf))> Would something like this work, like running rest_api_init hook before init hook fires, not sure if this is valid? add_action( \"init\", \"rest_api_init\");function rest_api_init(){add_action(\"rest_api_init\", function () {register_rest_route(\"jwt-auth/v1\",> Well, it\"ll either amount to the same thing or it won\"t work at all. It depends on what order each action fires. I\"m unsure of the order, but I think it\"ll amount to the same thing. That said, rest_api_init fires pretty early, it\"s hard to imagine output was generated before this unless it\"s by non-core code I\"m unaware of. I think it fires so early that the user has not been established yet. Are you sure the cookie not set is the problem? Or you assume so much because the user does not get logged in? If the user has not yet been established, if ($user):将失败,甚至不会尝试任何身份验证cookie集。 也许您只需钩住\"init\",然后在执行任何操作之前检查if ( defined(\"REST_REQUEST\")):。 是,锁链没有帮助。我使用phpstorm设置了一个调试器。不过,我注意到一件事,在下面的代码中: if ($user) {wp_clear_auth_cookie();wp_set_current_user($user->ID, $user->user_login);wp_set_auth_cookie($user->ID);do_action(\"wp_login\", $user->user_login);wp_safe_redirect($redirectUrlAfterLogin);exit();} else {return \"getUserFromValidToken failed!\";} 当它调用wp_set_auth_cookie时,我确实看到生成了两个cookie($authu cookie,$loggedu inu cookie)并进行了设置。但是,当重定向发生时,在wpu登录之后,浏览器将重定向到原始的> ,而不是提供的重定向URLhttps://mydocs.local/。 我在我的safari中只看到了这两个cookie: wordpressu testu cookie XDEBUGu SESSION - 这个回复是9个月前由bcworkz修改的。 我在我的网站上尝试了你的代码,效果如期。我使用了适合我的网站的变量值,你确定你使用的变量有合适的值吗?值得转储或记录值以进行双重检查。 @bcworkz我刚刚发现一件事,当我启用输出缓冲时,它似乎可以工作。这是否解释了我的代码中的某个地方过早地刷新了输出?是的,我已经确认变量设置正确。我的安装程序在mac、非WP站点(java服务器)和WP站点(使用本地堆栈)上本地运行。我不认为这有什么关系,只是想检查一下。 启用输出缓冲是一个很好的线索!它确认在执行代码之前正在生成某种输出。这不一定是由于缓冲区刷新造成的,某些东西可能直接通过echo或某种打印语句生成输出。 要缩小源代码范围,请尝试将主题切换到twenty20并停用所有插件,但包含auth cookie代码的插件除外。它现在应该可以工作了,因为核心WP不会产生这么早的输出。如果您仍然有问题,那么输出显然来自您的auth cookie代码所在的模块。 或者使用缓冲区来解决问题。设置cookie后,刷新并输出缓冲区中的任何内容。 好的,我将尝试细化它。我用的是twenty20插件。 如何在不将缓冲区添加到php.ini?我试过了ob.启动()并且它的行为方式不同。 我们使用WPEngine来托管WP站点,不幸的是,它们不允许我们编辑php.ini文件 看看https://www.php.net/manual/en/ref.outcontrol.php php.ini文件除非出于某种原因禁用了输出缓冲,否则不需要访问,这可能会中断大量的数据事情似乎不太可能发生。 @bcworkz谢谢你的链接。我必须更仔细地查看来自wordpress的警告: PHP Warning:Use of undefined constant n - assumed \"n\" (this will throw an Error in a future version of PHP) in /Users/arul/Local Sites/portal/app/public/wp-content/plugins/jwt-authenticator/JWT.php on line 36PHP Warning:Use of undefined constant H - assumed \"H\" (this will throw an Error in a future version of PHP) in /Users/arul/Local Sites/portal/app/public/wp-content/plugins/jwt-authenticator/JWT.php on line 39PHP Warning:Use of undefined constant a - assumed \"a\" (this will throw an Error in a future version of PHP) in /Users/arul/Local Sites/portal/app/public/wp-content/plugins/jwt-authenticator/JWT.php on line 54 这些警告导致了这个错误: PHP Warning:Cannot modify header information - headers already sent by (output started at /Users/arul/Local Sites/portal/app/public/wp-content/plugins/jwt-authenticator/JWT.php:36) in /Users/arul/Local Sites/portal/app/public/wp-includes/pluggable.php on line 987 PHP不喜欢这个代码: switch ($algorithm[0]) = \"ECDSA\";break;*/default: exit(\"RSA and ECDSA not implemented yet!\");} 我修复了这个代码,所以在switch语句中引用了case标签,这是所有这些警告的根本原因。我觉得没有更深入地研究这个问题很愚蠢。 它现在可以在本地工作,而不需要在配置中添加输出缓冲php.ini文件. (whew) 我的整个安装程序都在本地运行,没有任何问题(我使用本地堆栈进行开发,它在该安装程序中运行得非常好)。 我将尝试将其部署到我的主机提供商,看看这是否顺利。 谢谢你的帮助。非常感谢。 不客气。我很高兴你发现了问题

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1185

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1085

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this->id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:823

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this->id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:915

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:944

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:894

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:879

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:842

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1778

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1327