Cookie problem without www prefixed.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #1

    Cookie problem without www prefixed.

    A person logs in to http://www.my-site.com using his id and password.

    Then he removes the www and opens the page as http://my-site.com

    Now he needs to login again, as the cookie was set for www.my-site.com, while it should not be like this. I want that it works for both www.my-site.com and my-site.com when logged in once from either of these..

    How can I solve this?.. Is there any conventional way to do this or ...?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    You need to set the domain parameter in the setcookie function to, for example, .example.com.
    Note the single dot in front of the domain name. That should make it available on all sub-domains.

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Thank you Atli :)

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        If I want to use a variable instead of .example.com, what should I use?

        I tried ".".$_SERVE R['SERVER_NAME'], but not working.

        [EDIT: Actually working when online. Not working with localhost. So problem solved]

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          It's never good to rely on the $_SERVER super-global. It is generated by the HTTP server, rather then PHP itself, so you can never really be sure that a specific element exits.

          I would recommend just adding a constant, specifying the domain.
          You would only ever have to change it if you changed servers.

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            Originally posted by Atli
            It's never good to rely on the $_SERVER super-global. It is generated by the HTTP server, rather then PHP itself, so you can never really be sure that a specific element exits.

            I would recommend just adding a constant, specifying the domain.
            You would only ever have to change it if you changed servers.
            Ok, then I'll set a constant.

            Thank you :)

            Comment

            Working...