php Cookie在WordPress中重定向后丢失

b1payxdu  于 2023-05-05  发布在  PHP
关注(0)|答案(1)|浏览(160)

总之,我有一个我已经建立的表单,我调用我的插件中的一个函数来设置一个cookie,当表单被提交并重定向到一个页面时。通过测试,我可以看到cookie确实在表单提交时设置,但当重定向发生时,我丢失了cookie和与之相关的值。我一直在爬论坛,并试图调试这么长时间,我想我错过了一些超级简单的东西。我试过wp_redirectwp_safe_redirectheader Location:window.open()等。
如果我在重定向发生之前使用wp_die($_COOKIE),我可以看到我的cookie,所以我确定它发生在页面被重定向到另一个页面之后。
我怎么能有它的表单被提交,cookie被设置和重定向(到同一个网站上的不同页面)不删除cookie,以便我可以工作在重定向页面上的数据?

add_action('admin_post_action_configurator_form_submit', 'action_configurator_form_submit'); // logged in users
add_action('admin_post_nopriv_action_configurator_form_submit', 'action_configurator_form_submit'); // not logged in users

function action_configurator_form_submit () {
    // checking if nonce was submited and is valid, if not valid will exit
    check_ajax_referer('configurator_form_nonce', 'security');

    if(isset($_POST['base_sku'])){
        //store form entries as variables
        $base_Sku = $_POST['base_sku'];
        $type = $_POST['type'];
        $band = $_POST['band'];
        $polarization = $_POST['polarization'];
        $gain_Sku = $_POST['gain_sku'];
        $Az_Pattern = $_POST['azpattern'];
        $dual_Input = $_POST['dualinput'];
        $narrowband_Connector = $_POST['connector'];
        $beamtilt = $_POST['beamtilt'];
        $null_Fill = $_POST['nullfill'];
        $heavy_Duty = $_POST['heavyduty'];
        $invert_Mount = $_POST['invertmount'];
        $narrowband = $_POST['narrowband'];

        //Build sku group
        $antennaSku = $base_Sku . $type . $band . $polarization . $gain_Sku . $Az_Pattern;
        $fullSku = $antennaSku . '-' . $dual_Input . '-' . $narrowband . '-' . $narrowband_Connector . '-' . $beamtilt . '-' . $null_Fill . '-' . $heavy_Duty . '-' . $invert_Mount;

        $cookieValue = array(
            'base_Sku' => $base_Sku,
            'type' => $type,
            'band' => $band,
            'polarization' => $polarization,
            'gain_Sku' => $gain_Sku,
            'Az_Pattern' => $Az_Pattern,
            'dual_Input' => $dual_Input,
            'narrowband_Connector' => $narrowband_Connector,
            'beamtilt' => $beamtilt,
            'null_Fill' => $null_Fill,
            'heavy_Duty' => $heavy_Duty,
            'invert_Mount' => $invert_Mount,
            'narrowband' => $narrowband,
            'generatedSku' => $fullSku,
        );

        //Find product by matching title based on Sku generated from form
        $page = get_page_by_post_name($antennaSku, OBJECT, 'product');

        //Build cookie Usage: $data = json_decode($_COOKIE['antennasNow'], true);
        unset($_COOKIE['antennasNow']);
        setcookie('antennasNow', json_encode($cookieValue), time()+3600);

        if(!empty($page)){
            //If matching product is found, redirect to it
            $url = get_permalink($page);
            redirect_to_antenna_product($url);
        } else {
            //Otherwise, redirect to a fallback page
            $url = '/selector-support';
            redirect_to_antenna_product($url);
        }
    }
}

function redirect_to_antenna_product($url){
        wp_safe_redirect($url);
        exit();
}

function get_page_by_post_name($post_name, $output = OBJECT, $post_type = 'post' ){
    global $wpdb;
    $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $post_name, $post_type ) );

    if ( $page ) return get_post( $page, $output );

    return null;
}

add_action('init','get_page_by_post_name');

如果有帮助,这里是表单操作:

<form action="' . esc_url(admin_url('admin-post.php')) . '" method="post" id="configurator">
fykwrbwg

fykwrbwg1#

试试这个:

setcookie( 'antennasNow', json_encode($cookieValue), time()+3600, '/' );

相关问题