PHP Subdomain session disappearing

2019-09-09 20:24发布

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条回答
▲ chillily
2楼-- · 2019-09-09 20:37

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.

查看更多
登录 后发表回答