note 36202 added to function.setcookie

From: Date: Tue, 30 Sep 2003 20:13:19 +0000
Subject: note 36202 added to function.setcookie
Groups: php.notes 
Request: Send a blank email to php-notes+get-57509@lists.php.net to get a copy of this message
When using your cookies on a webserver that is not on the standard port 80, you should NOT include the :[port] in the "Cookie domain" parameter, since this would not be recognized correctly. I had the issue working on a project that runs on multiple servers (development, production, etc.). One of the servers is running on a different port (together with other websites that run on the same server but on different ports). This is what I came up with to avoid mixing up the authentication cookies between the different sites. Works like a breeze with no modification necessary between moving from server to server: // ****************************************** // ** define cookie domain for correct setting of the cookie // get cookie from HTTP_HOST env. variable $this->_cookie_domain = $_SERVER['HTTP_HOST']; // Strip www from the Host, for the cookie to work on subdomains if (strtolower(substr($this->_cookie_domain, 0, 3)) == 'www') $this->_cookie_domain = substr($this->_cookie_domain, 3); //Replace special characters in the HTTP host to use as cookie name $this->_cookie_name = str_replace(array('.',':','-'), '_', $this->_cookie_domain); // Check if a port is used, and if it is, strip that info $uses_port = strpos($this->_cookie_domain, ':'); if ($uses_port) $this->_cookie_domain = substr($this->_cookie_domain, 0, $uses_port); // ****************************************** The result is this: * On server "www.example.com", the cookie domain is ".example.com", the cookie name "_example_com". * On server "10.0.0.5:8300", the cookie domain is "10.0.0.5", the cookie name is "10_0_0_5_8300". Hope this helps somebody in the situation. Joerg. ---- Manual Page -- http://www.php.net/manual/en/function.setcookie.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+36202 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+36202&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+36202&report=yes

« previous php.notes (#57509) next »