PHP Subdomain session disappearing

馋奶兔 提交于 2020-01-06 14:45:39

问题


index.php

session_start();

if(!isset($_GET['n'])) {
  $_SESSION['foo'] = "bar";
  header("Location: /index.php?n=D");
}

echo "FOO: ".$_SESSION['foo'];

This page index.php is used in a subdomain. The subdomain is a folder in the root domain, i.e.

 - WWW
    - SUB DOMAIN
      - index.php

The session variable foo is set on page call, then checked after a redirect header - and it's gone.

I've tried:

  • ini_set('session.cookie_domain','.example.com');
  • Various combinations of session_set_cookie_params()
  • Location: http://subdomain.example.com
  • Tearing my hair out

but nothing seems to be working. Seems like a very simple problem - can someone help me find my lost cookie / session / variable ?

EDIT

var_dump(session_get_cookie_params()) yields

array(5) { ["lifetime"]=> int(0) ["path"]=> string(11) "/" ["domain"]=>
 string(23) ".example.com" ["secure"]=> bool(false) ["httponly"]=> bool(false) } 

My URL has a dash - in it (my-example.com). Could this be the problem?

Also tried setting session_name() before params. Still not working.


回答1:


After five hours of trying myself, I finally contacted my hosting provider and they had a look.

The verdict? session.save_path in php.ini had not been set. The script above is now running properly.

If someone cares to elaborate on the whys and hows and whatnot, I'll be happy to give them the solution, otherwise I'll have to accept my own.



来源:https://stackoverflow.com/questions/4301185/php-subdomain-session-disappearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!