duai4512 2016-06-20 05:52
浏览 54
已采纳

在wordpress中添加会话

I want to add session and retrieve value from it in wordpress. I am using subdomain for my website. I have added code in header.php file of a theme to retrive a subdomain and put it in session. I have started session and put the value in it. When I use subdomain in url session retrives session value but without subdomain name, it doesn't works, even session have that value. Following are my code:

$url = $_SERVER['SERVER_NAME'];
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['path']);
$subdomain = $host[0];

$wp_session = WP_Session::get_instance();
if(isset($wp_session['user_subdomain']))
{
?>
<script>alert('<?php echo "Check first session Set ".$wp_session['user_subdomain']; ?>');</script>
<?php
}
else
{
?>
<script>alert('<?php echo "Check first session Not Set "; ?>');</script>
<?php
}
if($wp_session['user_subdomain']=="test"){
?>
<script>alert('<?php echo "session ".$wp_session['user_subdomain']; ?>');</script>
<?php
$red_url=$wp_session['user_subdomain'].".domain.com";
?>
<script>alert('<?php echo "RED ".$red_url; ?>');</script>
<?php
header("location:$red_url");
}
else if($subdomain=="test"){
$red_url=$_SERVER['SERVER_NAME'];
$wp_session['user_subdomain']=$subdomain;
?>
<script>alert('<?php echo "get ".$subdomain; ?>');</script>
<script>alert('<?php echo "check set session ".$wp_session['user_subdomain']; ?>');</script>
<?php
header("location:$red_url");

}

I have added '_SESSION' in wp_unregister_GLOBALS function, I found it somewhere on google.

  • 写回答

2条回答 默认 最新

  • doudui5753 2017-01-30 11:46
    关注

    We give priority to set session. The following code will works fine.

    add_action('init', 'myStartSession', 1);
    
    function myStartSession() {
        if(!session_id()) {
            session_start();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)