Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Need to get cookie domain

    Hello,

    I set my cookies using setcookie like this for exp:

    PHP Code:
    setcookie("username"$_SESSION['username'], 2147483647"/"".$cookiedomain"0); 
    where $cookiedomain is my domain name without www, like mydomain.com, mysite.net etc.

    My question is, can I get this variable automatically from an environment or server variable? $_SERVER["HTTP_HOST"] gives something like www.mydomain.com and I could trim it but will it give correct value on every server? And, which one is more reliable, ENV or SERVER?

    Thanks,

  • #2
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    try this, this has worked for me plenty of times

    PHP Code:
    $cookiedomain $_SERVER["SERVER_NAME"];

    setcookie("username"$_SESSION['username'], 2147483647"/"".$cookiedomain"0); 
    good luck

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Hi, thanks for the reply!

    You determine the $cookiedomain as the value of $_SERVER['SERVER_NAME']. I guess this may work for you cause you always used the same server or a shared hosting? Maybe the SERVER_NAME can be different something than a domain name.

    And, your cookie will be useful only on www domain. I want it to be useful for people without www domain too.

    So, I wrote the following:

    PHP Code:

    $cookiedomain 
    $_SERVER['HTTP_HOST'];
    if(
    strtolower(substr($cookiedomain04)) == 'www.') {
        
    $cookiedomain substr($cookiedomain4);
    }
    $cookiedomain '.' $cookiedomain;

    setcookie("username"$_SESSION['username'], 2147483647"/"$cookiedomain0); 

  • #4
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    it shouldn't do that, it takes the server name from the url so if they go http://mysite.com/ it will output mysite.com if it is http://www.mysite.com the output will be www.mysite.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •