note 36202 modified in function.setcookie by victor

From: Date: Sat, 27 Mar 2004 20:17:17 +0000
Subject: note 36202 modified in function.setcookie by victor
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-67246@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). --was-- 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. http://www.php.net/manual/en/function.setcookie.php

« previous php.notes (#67246) next »