Search code examples
phpwordpresssessioniissubdirectory

PHP Session Variable Set in Root not Visible in Subdirectory


I have Wordpress running on windows shared hosting (IIS) w/ Godaddy. Wordpress is running out of it's own subdirectory "/wordpress". If I set a PHP session value e.g. $_SESSION["test1"] = "value set from root: /"; in PHP from root, I can see the session's value fine as long as my PHP code is running out of root. If I try and access this session value from the /wordpress folder, it is not found.

Vice versa applies. If I set a session value under /wordpress and its subdirectories, I can view the session and its values great as long as I operate within the /wordpress folder. If I try and access the session value which was set from within /wordpress in root it is not accessible.

I've checked the session ids and they are both the same from root and from the /wordpress folder. I've inspected the cookies set from both and they have the same path & domain.

I'm at a loss why setting PHP session values from root versus the /wordpress folder would not be global. Any help would be appreciated. Thanks.

/set_session.php

<?php
session_start();
$_SESSION["test1"] = "value set from root: /";
?>

/wordpress/set_session.php

<?php
session_start();
$_SESSION["test2"] = "value set from wordpress subdirectory: /wordpress";
?>

view_session.php (I run this from root and /wordpress)

<?php
session_start();
echo "<h3> PHP List All Session Variables</h3>";
   foreach ($_SESSION as $key=>$val)
    echo $key." ".$val."<br/>";
?>

If I run the view session script from /view_session.php I only see a value for "test1" as it was set from /set_session.php.

If I run the view session script from /wordpress/view_session.php I only see the value for "test2" as it was set from /wordpress/set_session.php.


Solution

  • First, let me say GoDaddy support is useless. They took the stance that because there was no official "error" that they could not help me and it was not their fault regardless of having sample basic code demonstrating the issue.

    When I installed WordPress, I used their Plesk automated install option to install WP for me. It installed WordPress to its own sub-directory. This is fine, however their install processes made the /wordpress folder its own virtual directory which causes all content within it to run under its own app pool/thread. This caused the different sessions between the /wordpress directory and other directories within the site.