drfif48428 2019-02-04 16:28
浏览 80
已采纳

Wordpress:程序化登录无效。 此外,wp_redirect无效。 不知道为什么

[ Please forget about security concerns for a moment... ]

My goal is to create an invitation page where people land on after being invited by an email with a unique link. On that page they choose a password using a basic HTML form, which posts into the very same page.

When posted, my goal is to programmatically log the user in (the backend knows the email + the password based on the unique invite code) and redirect that user to another page, as a logged in user.

my code has NO EFFECT. The user is not logged in (I can see that by refreshing another page in my web app in another browser tab, where the header differs if the user is logged in). Also-- the redirect has no effect. We never navigate out of the page we posted to.

Here's the code. It is being executed in the POST portion of the page:

 $creds = array(
        'user_login'    => $user->user_email,  // VERIFIED- CORRECT
        'user_password' => $password,          // VERIFIED- CORRECT
        'remember'      => true
    );

    $user = wp_signon( $creds, false );

    if ( is_wp_error( $user ) ) {
        echo $user->get_error_message(); // DOESN'T GET HERE
    }

    wp_redirect('http://mywebapp.loc/faq'); // DOESN'T REDIRECT
    die();

I would appreciate any pointer. Obviously I am doing something wrong.

  • 写回答

1条回答 默认 最新

  • dongli4711 2019-02-04 16:52
    关注

    I was calling wp_signon and wp_redirect after the headers have been sent, and as redirect + session cookies are sent in those headers -- it was too late. Moving that piece of code BEFORE the headers resolved the issue.

    for me it was as simple as moving this code down the php file, until after the wp_signon and wp_redirect section:

    <?php
     get_header();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同