duanbi1983 2018-08-14 15:13
浏览 73
已采纳

写入数据库,并在同一PHP页面加载中检索更新的数据?

My website runs on WordPress, and includes an account activation system that works like this:

  1. User creates account, gets logged in immediately and is shown the home page.
  2. A function checks the database to see if the account is activated, and if not, displays a message on the home page asking to click the link in the verification email.
  3. User opens their email and clicks the activation link. (example: https://example.com/?action=activate&user=swen&key=1234)
  4. On opening this page, a function checks if an action argument is set and runs the account activation function, which then updates the account to 'activated'.
  5. The rest of WP is executed, but the function checking for account activation still shows that the account has not been activated.
  6. Upon browsing to https://example.com again without query vars, the message disappears, showing that the above example URL, did in fact work to activate the account.

Is it possible that the time between writing data to the database, and retrieving that same updated data is too short?


UPDATE:

Wondering if MySQL caches the data in the same pageload. I tried flushing the WordPress cache before retrieving the updated user role, no luck.

Action that checks URL and runs the activate_user function:

I tried doing this as early as possible with the setup_theme hook to ensure it's run before the theme is loaded.

add_action('setup_theme', 'ns_activate_user_action');
function ns_activate_user_action() {
    // Check if all action parameters are present
    if ( !ns_validate_action('activate', array('user', 'key')) ) {
        return;
    }

    // Activate user by key, runs the "activate_user" function
    $activate_user = activate_user_by_key($_GET['user'], $_GET['key']);

    // At this point the user role should have changed
    // and the account be activated
}

Function that activates account:

function activate_user($user) {
    // ...

    $user->add_role( 'member' );
    $user->remove_role( 'member_pending' );

    // ...

    return $user;       
}

Function that checks if account is activated:

function is_user_activated($user) {

    // ...

    if ( in_array( 'member_pending', (array) $user->roles ) ) {
        return false;
    }

    return true;
}

Custom action displays activation message, is called with do_action() in theme file:

add_action('ns_after_header', 'ns_activate_account_notice_front_page');
function ns_activate_account_notice_front_page() {
    if ( is_user_logged_in() && is_front_page() && !is_user_activated() ) {
        // ... Echos message asking user to click activation link in email
        // Checking the user role here with wp_get_current_user()->roles
        // still shows that the user is in the "member_pending" role (inactive)
    }
}
  • 写回答

2条回答 默认 最新

  • dongping9475 2018-08-15 10:26
    关注

    So as it turns out, it was an issue with WordPress, most likely something to do with cache. Check out this question on why it happens for more info.

    Updated user role inncorrect when using wp_get_current_user()

    And to answer my own question:

    Yes, you can write to the database, and retrieve the updated information in the same PHP page load.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵