mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-26 22:08:55 +00:00

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@166167 c90b9560-bf6c-de11-be94-00142212c4b1
364 lines
14 KiB
XML
364 lines
14 KiB
XML
<?xml version="1.0" encoding="iso-8859-1"?>
|
|
<!-- $Revision: 1.31 $ -->
|
|
<!-- splitted from ./en/functions/http.xml, last change in rev 1.2 -->
|
|
<refentry id="function.setcookie">
|
|
<refnamediv>
|
|
<refname>setcookie</refname>
|
|
<refpurpose>Send a cookie</refpurpose>
|
|
</refnamediv>
|
|
<refsect1>
|
|
<title>Description</title>
|
|
<methodsynopsis>
|
|
<type>bool</type><methodname>setcookie</methodname>
|
|
<methodparam><type>string</type><parameter>name</parameter></methodparam>
|
|
<methodparam choice="opt"><type>string</type><parameter>value</parameter></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>expire</parameter></methodparam>
|
|
<methodparam choice="opt"><type>string</type><parameter>path</parameter></methodparam>
|
|
<methodparam choice="opt"><type>string</type><parameter>domain</parameter></methodparam>
|
|
<methodparam choice="opt"><type>bool</type><parameter>secure</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
<function>setcookie</function> defines a cookie to be sent along
|
|
with the rest of the HTTP headers. Like other headers, cookies
|
|
must be sent <emphasis>before</emphasis> any output from your
|
|
script (this is a protocol restriction). This requires that you
|
|
place calls to this function prior to any output, including
|
|
<literal><html></literal> and <literal><head></literal> tags
|
|
as well as any whitespace. If output exists prior to calling this
|
|
function, <function>setcookie</function> will fail and return &false;.
|
|
If <function>setcookie</function> successfully runs, it will return
|
|
&true;. This does not indicate whether the user accepted the cookie.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
As of PHP 4, you can use output buffering to send output prior to the
|
|
call of this function, with the overhead of all of your output to the
|
|
browser being buffered in the server until you send it. You can do this
|
|
by calling <function>ob_start</function> and
|
|
<function>ob_end_flush</function> in your script, or setting the
|
|
<literal>output_buffering</literal> configuration directive on in your
|
|
&php.ini; or server configuration files.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
All the arguments except the <parameter>name</parameter> argument
|
|
are optional. You may also replace an argument with an empty string
|
|
(<emphasis>""</emphasis>) in order to skip that
|
|
argument. Because the <parameter>expire</parameter>
|
|
argument is integer, it cannot
|
|
be skipped with an empty string, use a zero (<emphasis>0</emphasis>)
|
|
instead. The following table explains each parameter of the
|
|
<function>setcookie</function> function, be sure to read the
|
|
<ulink url="&spec.cookies;">Netscape cookie specification</ulink> for
|
|
specifics on how each <function>setcookie</function> parameter works
|
|
and <ulink url="&url.rfc;2965">RFC 2965</ulink> for additional
|
|
information on how HTTP cookies work.
|
|
</para>
|
|
<para>
|
|
<table>
|
|
<title><function>setcookie</function> parameters explained</title>
|
|
<tgroup cols="3">
|
|
<thead>
|
|
<row>
|
|
<entry>Parameter</entry>
|
|
<entry>Description</entry>
|
|
<entry>Examples</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry><parameter>name</parameter></entry>
|
|
<entry>
|
|
The name of the cookie.
|
|
</entry>
|
|
<entry>
|
|
'cookiename' is called as <varname>$_COOKIE['cookiename']</varname>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry><parameter>value</parameter></entry>
|
|
<entry>
|
|
The value of the cookie. This value is stored on the clients
|
|
computer; do not store sensitive information.
|
|
</entry>
|
|
<entry>
|
|
Assuming the <parameter>name</parameter> is 'cookiename', this
|
|
value is retrieved through <varname>$_COOKIE['cookiename']</varname>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry><parameter>expire</parameter></entry>
|
|
<entry>
|
|
The time the cookie expires. This is a Unix timestamp so is
|
|
in number of seconds since the epoch. In otherwords, you'll
|
|
most likely set this with the <function>time</function> function
|
|
plus the number of seconds before you want it to expire. Or
|
|
you might use <function>mktime</function>.
|
|
</entry>
|
|
<entry>
|
|
<literal>time()+60*60*24*30</literal> will set the cookie to
|
|
expire in 30 days. If not set, the cookie will expire at
|
|
the end of the session (when the browser closes).
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry><parameter>path</parameter></entry>
|
|
<entry>
|
|
The path on the server in which the cookie will be available on.
|
|
</entry>
|
|
<entry>
|
|
If set to <literal>'/'</literal>, the cookie will be available
|
|
within the entire <parameter>domain</parameter>. If set to
|
|
<literal>'/foo/'</literal>, the cookie will only be available
|
|
within the <literal>/foo/</literal> directory and all
|
|
sub-directories such as <literal>/foo/bar/</literal> of
|
|
<parameter>domain</parameter>. The default value is the
|
|
current directory that the cookie is being set in.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry><parameter>domain</parameter></entry>
|
|
<entry>
|
|
The domain that the cookie is available.
|
|
</entry>
|
|
<entry>
|
|
To make the cookie available on all subdomains of example.com
|
|
then you'd set it to <literal>'.example.com'</literal>. The
|
|
<literal>.</literal> is not required but makes it compatible
|
|
with more browsers. Setting it to <literal>www.example.com</literal>
|
|
will make the cookie only available in the <literal>www</literal>
|
|
subdomain. Refer to tail matching in the
|
|
<ulink url="&spec.cookies;">spec</ulink> for details.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry><parameter>secure</parameter></entry>
|
|
<entry>
|
|
Indicates that the cookie should only be transmitted over a
|
|
secure HTTPS connection. When set to &true;, the
|
|
cookie will only be set if a secure connection exists. The default
|
|
is &false;.
|
|
</entry>
|
|
<entry>
|
|
<literal>0</literal> or <literal>1</literal>
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</table>
|
|
</para>
|
|
<para>
|
|
Once the cookies have been set, they can be accessed on the next page load
|
|
with the <link linkend="reserved.variables.cookies">$_COOKIE</link> or
|
|
<varname>$HTTP_COOKIE_VARS</varname> arrays. Note,
|
|
<link linkend="language.variables.superglobals">autoglobals</link>
|
|
such as <varname>$_COOKIE</varname> became available in PHP
|
|
<ulink url="&url.php.release4.1.0;">4.1.0</ulink>.
|
|
<varname>$HTTP_COOKIE_VARS</varname> has existed since PHP 3. Cookie
|
|
values also exist in <link linkend="reserved.variables.request">
|
|
$_REQUEST</link>.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
If the PHP directive <link linkend="ini.register-globals">register_globals</link>
|
|
is set to <literal>on</literal> then cookie values will also be made into
|
|
variables. In our examples below, <varname>$TextCookie</varname> will
|
|
exist. It's recommended to use <varname>$_COOKIE</varname>.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
Common Pitfalls:
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Cookies will not become visible until the next loading of a page that
|
|
the cookie should be visible for. To test if a cookie was successfully
|
|
set, check for the cookie on a next loading page before the cookie
|
|
expires. Expire time is set via the <parameter>expire</parameter>
|
|
parameter. A nice way to debug the existence of cookies is by
|
|
simply calling <literal>print_r($_COOKIE);</literal>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Cookies must be deleted with the same parameters as they were set with.
|
|
If the value argument is an empty string (""), and all other arguments
|
|
match a previous call to setcookie, then the cookie with the specified
|
|
name will be deleted from the remote client.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Cookies names can be set as array names and will be available to your
|
|
PHP scripts as arrays but separate cookies are stored on the users
|
|
system. Consider <function>explode</function> or
|
|
<function>serialize</function> to set one cookie with multiple names
|
|
and values.
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</para>
|
|
<simpara>
|
|
In PHP 3, multiple calls to <function>setcookie</function> in the same
|
|
script will be performed in reverse order. If you are trying to
|
|
delete one cookie before inserting another you should put the
|
|
insert before the delete. As of PHP 4, multiple calls to
|
|
<function>setcookie</function> are performed in the order called.
|
|
</simpara>
|
|
<para>
|
|
Some examples follow how to send cookies:
|
|
<example>
|
|
<title><function>setcookie</function> send example</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$value = 'something from somewhere';
|
|
|
|
setcookie("TestCookie", $value);
|
|
setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */
|
|
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
Note that the value portion of the cookie will automatically be
|
|
urlencoded when you send the cookie, and when it is received, it
|
|
is automatically decoded and assigned to a variable by the same
|
|
name as the cookie name. If you don't want this, you can use
|
|
<function>setrawcookie</function> instead if you are using PHP 5. To see
|
|
the contents of our test cookie in a script, simply use one of the
|
|
following examples:
|
|
</para>
|
|
<para>
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// Print an individual cookie
|
|
echo $_COOKIE["TestCookie"];
|
|
echo $HTTP_COOKIE_VARS["TestCookie"];
|
|
|
|
// Another way to debug/test is to view all cookies
|
|
print_r($_COOKIE);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
</para>
|
|
<para>
|
|
When deleting a cookie you should assure that the expiration date
|
|
is in the past, to trigger the removal mechanism in your browser.
|
|
Examples follow how to delete cookies sent in previous example:
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>setcookie</function> delete example</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// set the expiration date to one hour ago
|
|
setcookie ("TestCookie", "", time() - 3600);
|
|
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", ".example.com", 1);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
You may also set array cookies by using array notation in the
|
|
cookie name. This has the effect of setting as many cookies as
|
|
you have array elements, but when the cookie is received by your
|
|
script, the values are all placed in an array with the cookie's
|
|
name:
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>setcookie</function> and arrays</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// set the cookies
|
|
setcookie("cookie[three]", "cookiethree");
|
|
setcookie("cookie[two]", "cookietwo");
|
|
setcookie("cookie[one]", "cookieone");
|
|
|
|
// after the page reloads, print them out
|
|
if (isset($_COOKIE['cookie'])) {
|
|
foreach ($_COOKIE['cookie'] as $name => $value) {
|
|
echo "$name : $value <br />\n";
|
|
}
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
which prints
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
three : cookiethree
|
|
two : cookietwo
|
|
one : cookieone
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<note>
|
|
<para>
|
|
For more information on cookies, see Netscape's cookie
|
|
specification at <ulink
|
|
url="&spec.cookies;">&spec.cookies;</ulink> and
|
|
<ulink url="&url.rfc;2965">RFC 2965</ulink>.
|
|
</para>
|
|
<para>
|
|
You may notice the <parameter>expire</parameter> parameter takes on a
|
|
Unix timestamp, as opposed to the date format <literal>Wdy, DD-Mon-YYYY
|
|
HH:MM:SS GMT</literal>, this is because PHP does this conversion
|
|
internally.
|
|
</para>
|
|
<para>
|
|
<parameter>expire</parameter> is compared to the client's time which can
|
|
differ from server's time.
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<simpara>
|
|
Microsoft Internet Explorer 4 with Service Pack 1 applied does
|
|
not correctly deal with cookies that have their path parameter
|
|
set.
|
|
</simpara>
|
|
<simpara>
|
|
Netscape Communicator 4.05 and Microsoft Internet Explorer 3.x
|
|
appear to handle cookies incorrectly when the path and time
|
|
are not set.
|
|
</simpara>
|
|
</note>
|
|
<para>
|
|
See also <function>header</function>, <function>setrawcookie</function>
|
|
and the <link linkend="features.cookies">cookies section</link>.
|
|
</para>
|
|
</refsect1>
|
|
</refentry>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"../../../../manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|