Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

// redirect request to another address
header("Location: http://www.leonatkinson.com/");;)
?>


boolean setcookie(string name, string value, integer expire,
string path, string domain, integer secure)


Use setcookie to send a cookie to the browser. Cookies are sent as headers during an
HTTP connection. Since cookie headers are more complex than other headers, it is nice
to have a function specifically for sending cookies. Keep in mind that all headers must be
sent prior to any content. Also, calling setcookie does not create a PHP variable until the
cookie is sent back by the browser on the next page load.


If setcookie is called with only the name argument, the cookie will be deleted from the
browser's cookie database. Otherwise, a cookie will be created on the client browser with
the name and value given.


The optional expire argument sets a time when the cookie will automatically be deleted
by the browser. This takes the form of seconds since January 1, 1970. PHP converts this
into Greenwich Mean Time and the proper form for the Set-Cookie header. If the expire
argument is omitted, the browser will delete the cookie when the session ends. Usually
this means when the browser application is shut down.


The path and domain arguments are used by the browser to determine whether to send
the cookie. The hostname of the Web server is compared to the domain. If it is left empty,
the complete hostname of the server setting the cookie is used. The path is matched
against the beginning of the path onthe server to the document. The cookie specification
requires that domains contain two periods. This is to prevent scripts that get sent to every
top-level domain (.com, .edu, .net). It also prevents a domain value of
leonatkinson.com. Just remember to add a leading dot.


The secure argument is used to tell the browser to send the cookie only over secure
connections which use Secure Socket Layers. Use a value of 1 to denote a secure cookie.


Like other headers, those created by the setcookie function are pushed onto a stack,
which causes them to be sent in reverse order. If you set the same cookie more than once,
the first call to setcookie will be executed last. Most likely, this isn't what you intend.
Keep track of the value you intend to set as the value of the cookie and call setcookie
once.


Netscape, which developed cookies, offers more information about them in a document
titled Persistent Client State: HTTP Cookies. Its URL is


http://developer.netscape.com/docs/manuals/communicator/jsguide4/cooki
es.htm
.

Free download pdf