saving variables after redirect

admin2025-06-02  3

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' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748866924a314378.html

最新回复(0)