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条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)