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.