note 81398 added to function.setcookie
| From: | isooik at gmail-antispam dot com | Date: | Tue, 26 Feb 2008 13:18:03 +0000 |
| Subject: | note 81398 added to function.setcookie | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136497@lists.php.net to get a copy of this message | ||
Here's a more advanced version of the php setcookie() alternative function:
<?php
/**
* A better alternative (RFC 2109 compatible) to the php setcookie() function
*
* @param string Name of the cookie
* @param string Value of the cookie
* @param int Lifetime of the cookie
* @param string Path where the cookie can be used
* @param string Domain which can read the cookie
* @param bool Secure mode?
* @param bool Only allow HTTP usage?
* @return bool True or false whether the method has successfully run
*/
function createCookie($name, $value='', $maxage=0, $path='',
$domain='', $secure=false, $HTTPOnly=false)
{
$ob = ini_get('output_buffering');
// Abort the method if headers have already been sent, except when output buffering has been
enabled
if ( headers_sent() && (bool) $ob === false || strtolower($ob) == 'off' )
return 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);
// Add the dot prefix to ensure compatibility with subdomains
if ( substr($domain, 0, 1) != '.' ) $domain = '.'.$domain;
// Remove port information.
$port = strpos($domain, ':');
if ( $port !== false ) $domain = substr($domain, 0, $port);
}
// Prevent "headers already sent" error with utf8 support (BOM)
//if ( utf8_support ) header('Content-Type: text/html; charset=utf-8');
header('Set-Cookie: '.rawurlencode($name).'='.rawurlencode($value)
.(empty($domain) ? '' : '; Domain='.$domain)
.(empty($expires) ? '' : '; Max-Age='.$maxage)
.(empty($path) ? '' : '; Path='.$path)
.(!$secure ? '' : '; Secure')
.(!$HTTPOnly ? '' : '; HttpOnly'), false);
return true;
}
?>
Regards,
Isaak
----
Server IP: 69.147.83.197
Probable Submitter: 213.49.228.92
----
Manual Page -- http://www.php.net/manual/en/function.setcookie.php
Edit -- https://master.php.net/note/edit/81398
Del: integrated -- https://master.php.net/note/delete/81398/integrated
Del: useless -- https://master.php.net/note/delete/81398/useless
Del: bad code -- https://master.php.net/note/delete/81398/bad+code
Del: spam -- https://master.php.net/note/delete/81398/spam
Del: non-english -- https://master.php.net/note/delete/81398/non-english
Del: in docs -- https://master.php.net/note/delete/81398/in+docs
Del: other reasons-- https://master.php.net/note/delete/81398
Reject -- https://master.php.net/note/reject/81398
Search -- https://master.php.net/manage/user-notes.php