Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 2437

php-manual-en-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>setcookie</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="index.html"><LINK
REL="UP"
TITLE="HTTP functions"
HREF="ref.http.html"><LINK
REL="PREVIOUS"
TITLE="headers_sent"
HREF="function.headers-sent.html"><LINK
REL="NEXT"
TITLE="Hyperwave functions"
HREF="ref.hyperwave.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="refentry"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.headers-sent.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="ref.hyperwave.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.setcookie"
></A
>setcookie</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN30596"
></A
><P
>    (PHP 3, PHP 4 )</P
>setcookie&nbsp;--&nbsp;Send a cookie</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN30599"
></A
><H2
>Description</H2
>boolean <B
CLASS="methodname"
>setcookie</B
> ( string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])<BR
></BR
><P
>&#13;     <B
CLASS="function"
>setcookie()</B
> defines a cookie to be sent along
     with the rest of the HTTP headers. Like other headers, cookies
     must be sent <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>before</I
></SPAN
> any output from your
     script (this is a protocol restriction). This requires that you
     place calls to this function prior to any output, including
     <TT
CLASS="literal"
>&#60;html&#62;</TT
> and <TT
CLASS="literal"
>&#60;head&#62;</TT
> tags
     as well as any whitespace. If output exists prior to calling this
     function, <B
CLASS="function"
>setcookie()</B
> will fail and return <TT
CLASS="constant"
><B
>FALSE</B
></TT
>.
     If <B
CLASS="function"
>setcookie()</B
> successfully runs, it will return
     <TT
CLASS="constant"
><B
>TRUE</B
></TT
>. This does not indicate whether the user accepted the cookie.
    </P
><P
>&#13;     All the arguments except the <TT
CLASS="parameter"
><I
>name</I
></TT
> argument
     are optional.  If only the name argument is present, the cookie
     by that name will be deleted from the remote client.  You may
     also replace an argument with an empty string
     (<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>""</I
></SPAN
>) in order to skip that
     argument.  Because the <TT
CLASS="parameter"
><I
>expire</I
></TT
> and
     <TT
CLASS="parameter"
><I
>secure</I
></TT
> arguments are integers, they cannot
     be skipped with an empty string, use a zero (<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>0</I
></SPAN
>) 
     instead.  The following table explains each parameter of the 
     <B
CLASS="function"
>setcookie()</B
> function, be sure to read the  
     <A
HREF="http://www.netscape.com/newsref/std/cookie_spec.html"
TARGET="_top"
>Netscape cookie specification</A
> 
     for specifics.
    </P
><P
>&#13;     <DIV
CLASS="table"
><A
NAME="AEN30640"
></A
><P
><B
>Table 1. <B
CLASS="function"
>setcookie()</B
> parameters explained</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
>Parameter</TH
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
>Description</TH
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
>Examples</TH
></TR
></THEAD
><TBODY
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="parameter"
><I
>name</I
></TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          The name of the cookie.
         </TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          'cookiename' is called as <TT
CLASS="varname"
>$_COOKIE['cookiename']</TT
>
         </TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="parameter"
><I
>value</I
></TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          The value of the cookie.  This value is stored on the clients
          computer; do not store sensitive information.
         </TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          Assuming the <TT
CLASS="parameter"
><I
>name</I
></TT
> is 'cookiename', this
          value is retrieved through <TT
CLASS="varname"
>$_COOKIE['cookiename']</TT
>
         </TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="parameter"
><I
>expire</I
></TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          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 <A
HREF="function.time.html"
><B
CLASS="function"
>time()</B
></A
> function
          plus the number of seconds before you want it to expire.  Or
          you might use <A
HREF="function.mktime.html"
><B
CLASS="function"
>mktime()</B
></A
>.
         </TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          <TT
CLASS="literal"
>time()+60*60*24*30</TT
> 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).
         </TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="parameter"
><I
>path</I
></TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          The path on the server in which the cookie will be available on.
         </TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          If set to <TT
CLASS="literal"
>'/'</TT
>, the cookie will be available
          within the entire <TT
CLASS="parameter"
><I
>domain</I
></TT
>.  If set to
          <TT
CLASS="literal"
>'/foo/'</TT
>, the cookie will only be available
          within the <TT
CLASS="literal"
>/foo/</TT
> directory and all
          sub-directories such as <TT
CLASS="literal"
>/foo/bar/</TT
> of
          <TT
CLASS="parameter"
><I
>domain</I
></TT
>.  The default value is the
          current directory that the cookie is being set in.
         </TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="parameter"
><I
>domain</I
></TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          The domain that the cookie is available.
         </TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          To make the cookie available on all subdomains of example.com
          then you'd set it to <TT
CLASS="literal"
>'.example.com'</TT
>.  The
          <TT
CLASS="literal"
>.</TT
> is not required but makes it compatible 
          with more browsers.  Setting it to <TT
CLASS="literal"
>www.example.com</TT
>
          will make the cookie only available in the <TT
CLASS="literal"
>www</TT
>
          subdomain.  Refer to tail matching in the 
          <A
HREF="http://www.netscape.com/newsref/std/cookie_spec.html"
TARGET="_top"
>spec</A
> for details.
         </TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
><TT
CLASS="parameter"
><I
>secure</I
></TT
></TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          Indicates that the cookie should only be transmitted over a 
          secure HTTPS connection.  When set to <TT
CLASS="literal"
>1</TT
>, the
          cookie will only be set if a secure connection exists.  The default
          is <TT
CLASS="literal"
>0</TT
>.
         </TD
><TD
ALIGN="LEFT"
VALIGN="MIDDLE"
>&#13;          <TT
CLASS="literal"
>0</TT
> or <TT
CLASS="literal"
>1</TT
>
         </TD
></TR
></TBODY
></TABLE
></DIV
>
    </P
><P
>&#13;     Once the cookies have been set, they can be accessed on the next page load
     with the <A
HREF="reserved.variables.html#reserved.variables.cookies"
>$_COOKIE</A
> or 
     <TT
CLASS="varname"
>$HTTP_COOKIE_VARS</TT
> arrays.  Note,
     <A
HREF="language.variables.predefined.html#language.variables.superglobals"
>autoglobals</A
> 
     such as <TT
CLASS="varname"
>$_COOKIE</TT
> became available in PHP
     <A
HREF="http://www.php.net/release_4_1_0.php"
TARGET="_top"
>4.1.0</A
>.
     <TT
CLASS="varname"
>$HTTP_COOKIE_VARS</TT
> has existed since PHP 3.  Cookie
     values also exist in <A
HREF="reserved.variables.html#reserved.variables.request"
>&#13;     $_REQUEST</A
>.
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
      If the PHP directive <A
HREF="configuration.directives.html#ini.register-globals"
>register_globals</A
>
      is set to <TT
CLASS="literal"
>on</TT
> then cookie values will also be made into
      variables.  In our examples below, <TT
CLASS="varname"
>$TextCookie</TT
> will
      exist.  It's recommended to use <TT
CLASS="varname"
>$_COOKIE</TT
>.
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     Common Pitfalls:
     <P
></P
><UL
><LI
><P
>&#13;        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 <TT
CLASS="parameter"
><I
>expire</I
></TT
> 
        parameter.  A nice way to debug the existence of cookies is by
        simply calling <TT
CLASS="literal"
>print_r($_COOKIE);</TT
>.
       </P
></LI
><LI
><P
>&#13;        Cookies must be deleted with the same parameters as they were set with.
       </P
></LI
><LI
><P
>&#13;        Cookies names can be set as array names and will be available to your 
        PHP scripts as arrays but seperate cookies are stored on the users 
        system.  Consider <A
HREF="function.explode.html"
><B
CLASS="function"
>explode()</B
></A
> or
        <A
HREF="function.serialize.html"
><B
CLASS="function"
>serialize()</B
></A
> to set one cookie with multiple names 
        and values.
       </P
></LI
></UL
>
    </P
><P
>&#13;     In PHP 3, multiple calls to <B
CLASS="function"
>setcookie()</B
> 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. In PHP 4, multiple calls to
     <B
CLASS="function"
>setcookie()</B
> are performed in the order called.
    </P
><P
>&#13;     Some examples follow how to send cookies:
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN30731"
></A
><P
><B
>Example 1. <B
CLASS="function"
>setcookie()</B
> send examples</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?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);
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     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.  To see the contents of our test
     cookie in a script, simply use one of the following examples:
     <DIV
CLASS="informalexample"
><A
NAME="AEN30736"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?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);
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     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:
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN30739"
></A
><P
><B
>Example 2. <B
CLASS="function"
>setcookie()</B
> delete examples</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", ".example.com", 1);
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     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:
     <DIV
CLASS="informalexample"
><A
NAME="AEN30744"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?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 =&#62; $value) {
        echo "$name : $value &#60;br /&#62;\n";
    }
}

/* which prints

three : cookiethree
two : cookietwo
one : cookieone

*/
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     For more information on cookies, see Netscape's cookie
     specification at <A
HREF="http://www.netscape.com/newsref/std/cookie_spec.html"
TARGET="_top"
>http://www.netscape.com/newsref/std/cookie_spec.html</A
>.
    </P
><P
>&#13;     Microsoft Internet Explorer 4 with Service Pack 1 applied does
     not correctly deal with cookies that have their path parameter
     set.
    </P
><P
>&#13;     Netscape Communicator 4.05 and Microsoft Internet Explorer 3.x
     appear to handle cookies incorrectly when the path and time
     are not set.
    </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.headers-sent.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="ref.hyperwave.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>headers_sent</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.http.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Hyperwave functions</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>