简体   繁体   中英

PHP Subdomain session disappearing

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, ie

 - 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM