note 70959 added to function.setcookie

From: Date: Mon, 06 Nov 2006 16:12:44 +0000
Subject: note 70959 added to function.setcookie
Groups: php.notes 
Request: Send a blank email to php-notes+get-119425@lists.php.net to get a copy of this message
The following code snippet combines abdullah's and Charles Martin's examples into a powerful combination function (and fixes at least one bug in the process): <?php function set_cookie_fix_domain($Name, $Value = '', $Expires = 0, $Path = '', $Domain = '', $Secure = false, $HTTPOnly = false) { if (!empty($Domain)) { // Fix the domain to accept domains with and without 'www.'. if (strtolower(substr($Domain, 0, 4)) == 'www.') $Domain = substr($Domain, 4); $Domain = '.' . $Domain; // Remove port information. $Port = strpos($Domain, ':'); if ($Port !== false) $Domain = substr($Domain, 0, $Port); } header('Set-Cookie: ' . rawurlencode($Name) . '=' . rawurlencode($Value) . (empty($Expires) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', $Expires) . ' GMT') . (empty($Path) ? '' : '; path=' . $Path) . (empty($Domain) ? '' : '; domain=' . $Domain) . (!$Secure ? '' : '; secure') . (!$HTTPOnly ? '' : '; HttpOnly'), false); } ?> Basically, if the domain parameter is supplied, it is converted to support a broader range of domains. This behavior may or may not be desireable (e.g. could be a security problem depending on the server) but it makes cookie handling oh-so-much-nicer (IMO). ---- Server IP: 64.71.164.2 Probable Submitter: 67.172.82.185 ---- Manual Page -- http://www.php.net/manual/en/function.setcookie.php Edit -- https://master.php.net/note/edit/70959 Del: integrated -- https://master.php.net/note/delete/70959/integrated Del: useless -- https://master.php.net/note/delete/70959/useless Del: bad code -- https://master.php.net/note/delete/70959/bad+code Del: spam -- https://master.php.net/note/delete/70959/spam Del: non-english -- https://master.php.net/note/delete/70959/non-english Del: in docs -- https://master.php.net/note/delete/70959/in+docs Del: other reasons-- https://master.php.net/note/delete/70959 Reject -- https://master.php.net/note/reject/70959 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119425) next »