setcookie(name [, value] [, time] [, path] [, domain] [, secure] [, httponly])
name – $string
value – [optional] $string
time – [optional] $integer default: till the end of the session
path – [optional] $string default: current directory
domain – [optional] $string default: current domain (e.g.
http://www.example.com)
secure – [optional] $boolean default: FALSE, does not require a secure
connection
httponly – [optional] $boolean default: FALSE, available to scripting
languages
14
PHP Reference: Beginner to Intermediate PHP5
Sets a cookie2
, visible to the server on the next page load. To send then
default value, use a set of single quotes (”) for each argument you want to
skip except the time argument, which should use 0 to send the default value.
In most cases, providing the name, value, time, and domain will cover most
uses (with ” for path).
Examples:
setcookie(‘Cookie’,’Set till end of this session’,0);
// This will display properly after the page has been reloaded
print_r($_COOKIE);
Array ( [Cookie] => Set till end of this session )
setcookie(‘Cookie’,’Set for 60 seconds for all subdomains of
example.com, such as www., mail., etc.’,time()+60,”,’.example.com’);
print_r($_COOKIE);
Array ( [Cookie] => Set for 60 seconds for all subdomains of
example.com, such as www., mail., etc. )
Some common times used for expiration:
time()+60*60*24 is equal to 1 day
time()+60*60*24*30 is equal to 30 days
time()-1 is one second in the past, used to expire/delete a cookie
setcookie(‘Cookie’,”,time()-1);
// expires the Cookie named ‘Cookie’. Note the empty string for value
urlencode($string)
Changes the formatting of $string to the proper format for passing through a
URL, such as part of a GET query, returning the new string.
Example:
$string = ‘Hello There! How are you?’;
echo urlencode($string);
Hello+There%21+How+are+you%3F
Question and answer is powered by AnsPress.io