Session cookie and www

前端 未结 5 974
南方客
南方客 2021-01-25 18:04

I would like to have my session work in my website when using www. AND when not using it. I\'ve read this thread: PHP cookie problem - www or without www

And this would

相关标签:
5条回答
  • 2021-01-25 18:26

    Using the correct hostname across all requests is important for sessions. However, if you are going to be accessing cookies across multiple subdomains then you can specify the domain parameter with a prepended period. I.e.,

    .example.com

    0 讨论(0)
  • 2021-01-25 18:35

    Use session_set_cookie_params function before calling session_start, it allows you to set the session domain and other things, set the domain to your domain prefixed with a . to make the session available to subdomains as well.

    0 讨论(0)
  • 2021-01-25 18:38

    You can reflect php.ini for this. Add this in php.ini so that your session cookie will be saved at the place to be accessible with or without www

    session.cookie_domain = .example.com 
    

    You can also try an alternate to do this

    ini_set("session.cookie_domain", ".example.com"); 
    

    And you can get the host name using $_SERVER['HTTP_HOST'] variable.

    0 讨论(0)
  • 2021-01-25 18:38

    PHP uses cookies for the session id, so thats really the same problem (and solution). Have a look at the session configuration.

    http://php.net/manual/en/session.configuration.php

    0 讨论(0)
  • 2021-01-25 18:45

    I've experienced this problem with my cookies and your link in your post is great solving that.

    So far, I'v never experienced difficulties with Sessions. It's independent from domain or sub domains, they are stored on the server-side.

    You can Set in a config file a constant parameter DOMAIN_NAME, or in the DB, in prevision for your cookies, and then modify it only once.

    0 讨论(0)
提交回复
热议问题