$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>saving variables after redirect|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

saving variables after redirect

matteradmin12PV0评论

Is there a way to save a variable after you redirect to another page. For example... I want to save the password submitted from my form into the variable $temp_passkey, and be able to access it on the next page that I redirected too.

I tried passing the argument through the url, but I don't want data to be visible in the url, so I need another way, or a way to hide the add_query_arg data from the url after the redirect.

function process_my_form() {

        if ( ! empty($_POST['password']) ) {
            $temp_passkey = ($_POST['password']);
        }

        wp_safe_redirect( esc_url_raw( add_query_arg( 'bid_passkey', $temp_passkey, '/secure-window' ) ) );
        exit;
    }
    add_action( 'admin_post_process_my_form', 'process_my_form' );
    add_action( 'admin_post_nopriv_process_my_form', 'process_my_form' );

Is there a way to save a variable after you redirect to another page. For example... I want to save the password submitted from my form into the variable $temp_passkey, and be able to access it on the next page that I redirected too.

I tried passing the argument through the url, but I don't want data to be visible in the url, so I need another way, or a way to hide the add_query_arg data from the url after the redirect.

function process_my_form() {

        if ( ! empty($_POST['password']) ) {
            $temp_passkey = ($_POST['password']);
        }

        wp_safe_redirect( esc_url_raw( add_query_arg( 'bid_passkey', $temp_passkey, '/secure-window' ) ) );
        exit;
    }
    add_action( 'admin_post_process_my_form', 'process_my_form' );
    add_action( 'admin_post_nopriv_process_my_form', 'process_my_form' );
Share Improve this question asked Feb 25, 2019 at 23:47 Emily ChildersEmily Childers 231 silver badge3 bronze badges 1
  • Why not just post the form directly to /secure-window and get the key from $_POST? Not sure why a redirect is necessary here. – Jacob Peattie Commented Feb 26, 2019 at 10:09
Add a comment  | 

1 Answer 1

Reset to default 0

You could store the password as a transient, then pass the transient key in the URL instead so it can be retrieved using that key on the next page.

function process_my_form() {

        if ( ! empty($_POST['password']) ) {
            $password = ($_POST['password']);
        } else {return;}

        /* Store Password via Transient API */
        $passkey = wp_generate_password(12, false);
        add_transient($passkey, $password, 300);

        wp_safe_redirect( esc_url_raw( add_query_arg( 'passkey', $passkey, '/secure-window' ) ) );
        exit;
}
add_action( 'admin_post_process_my_form', 'process_my_form' );
add_action( 'admin_post_nopriv_process_my_form', 'process_my_form' );

/* Example */
function process_password() {

    if ( !strstr($_SERVER['REQUEST_URI'], 'secure-window' ) {return;}
    if ( !isset($_POST['passkey']) ) {return;}

    $passkey = $_POST['passkey'];
    $password = get_transient($passkey);
    delete_transient($passkey);

    /* ... ... */
}
add_action( 'init', 'process_password' );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far