PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




setcookie

Name

setcookie — Send an HTTP cookie to the client.

Synopsis

void setcookie(name, value[, expiration][, path][, domain][, secure]);
string name: Cookie name
string value: Value of cookie
int expiration (optional): Expiration time
string path (optional): Server path
string domain (optional): Domain name
int secure (optional): Secure option

Returns

Void

Description

Sends an HTTP cookie to the client. Cookies are variables in key/value pairs sent by the server to the client, and are typically stored as text files. They also contain extra compulsory attributes as well as other optional attributes.

Cookies are a useful way to maintain information between browser sessions. The cookie is written to the client and retrieved by the server from the client on subsequent accesses to pages that match the specified path and domain set in the cookie. The name of the cookie is automatically assigned to a variable of the same name. For example, suppose a cookie was sent with the name MyCookie. When the cookie is retrieved, a variable is automatically created called $MyCookie, containing the cookie value. The optional domain and path can be skipped by using an empty string, while the expiration date and secure setting can be skipped by specifying a zero. Note that if the expiration date is set to zero, the lifetime of a cookie is only within the browser session.

The cookie is sent along with other HTTP headers and must be assigned before any other output is sent to the client. This is similar in behavior to the header() function, as the cookie data forms part of the server response to the client. Most common browsers have a security feature that allows you to see what cookies are being received by your machine. This can be an excellent way to test how cookies are written and retrieved. For more information on cookies, visit the Yahoo listing on cookies or view the Netscape cookie specification at http://www.netscape.com/newsref/std/cookie_spec.html.

There are various issues when using cookies in scripts. For example, some users avoid accepting cookies to maintain security. Having a backup system without cookies is always a good idea. Additionally, any GET or POST data with the same name as the cookie data will have preference within PHP. This can be altered by changing the GET/POST/Cookie order in the PHP initialization file (php.ini).

Finally, in addition to being able to access the value of the cookie by using the cookie name, cookie values are available in PHP's arrays of HTTP input. In the folowing example, the value of the cookie can be accessed using $HTTP_COOKIE_VARS["MyCookie"].

Version

Existing since versions 3.0 and 4.0

Example

Example 460. Send a cookie

$cookie_val = "Mmmm... cookies.";
setcookie("MyCookie", $cookie_val, time()+3600*24);
echo $MyCookie;



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.