【问题标题】:WordPress - Session values changes from page to pageWordPress - 会话值从页面到页面的变化
【发布时间】:2021-05-14 06:57:40
【问题描述】:

functions.php

<?php
if ( !defined('ABSPATH') ){ die(); }

ob_start();
if (!session_id()) {
    session_start();
}

plugins/bestellung/bestellung.php

<?php
add_shortcode('disp_bestellung_sidebar', 'display_side');
function display_side() {
    if($_SESSION["ptitle"] != "") {
        echo '<h3 class="bestellung_sidebar_title">Product</h3>';
        return ob_get_clean();
    }
}

plugins/bestellung/bridge.php

if (isset($_POST['prtitle'])) {
    $_SESSION["ptitle"] = $_POST['prtitle'];
}
header( 'Location: /bestellung/' );

bridge.php 中,我可以获得 $_SESSION["ptitle"] 及其值。 但是在它重定向到另一个页面之后,即“/bestellung/”,在此之前已经设置了其他键/值。

为什么会话中的值变成了别的东西?

【问题讨论】:

    标签: php wordpress session plugins shortcode


    【解决方案1】:

    您应该首先检查每个页面以查看会话是否已开始:

    if( ! session_id() ) {
        session_start();
    }
    
    if ( isset( $_POST['prtitle'] ) ) {
        $_SESSION["ptitle"] = $_POST['prtitle'];
    }
    
    wp_safe_redirect( home_url() . '/bestellung/' );
    exit;
    ......
    
    add_shortcode( 'disp_bestellung_sidebar', 'display_side' );
    function display_side() {
    
        if( ! session_id() ) {
            session_start();
        }
    
        if( isset( $_SESSION["ptitle"] ) &&  $_SESSION["ptitle"] != "" ) {
            echo '<h3 class="bestellung_sidebar_title">Product</h3>';
            return ob_get_clean();
        }
    }
    

    【讨论】:

    • 我已经设置了,但是没有用。我已将其设置在wp-configfunctions 和每个单独的文件中。没有运气。
    • 可能是重定向问题。为什么不使用 WordPress 重定向功能?请查看wp_safe_redirect
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    相关资源
    最近更新 更多