The setcookie() function, used to set one cookie at a time, expects six arguments:
This next line is an example of a cookie called id with a value of 55sds809892jjsj2. This particular cookie expires in four hours (the current time plus 14,400 seconds), and it is valid for any page below the document root on the domain yourdomain.com.
| PHP Code |
|---|
setcookie("id", "55sds809892jjsj2", time()+14400, "/" ,".yourdomain.com",0);
|
| Select all |
If you want to specify an expiration date or time, the easiest way is to tell PHP to count forward for you, and then place a value in the expiration slot within the setcookie() function. This value should be a Unix time integer (the number of seconds since January 1, 1970), which you can get using the time() function with additional seconds added to it.
Setting an expiration date on your cookies builds in some extra assurances of the validity of your users. If you set your cookie without a time limit, it will automatically expire when the users close their browsers. This is useful when users are sharing computers; you don't want the next user to have all the access afforded by the previous user's cookie. Similarly, you might want to set a cookie for only 15 minutes, if you are building an online store that allows you to receive a discount on everything purchased in the first 15 minutes of your users' visits.
The table below shows some common uses of time()+n within the setcookie() function:
| Value | Definition |
|---|---|
| time()+60 | One minute from the current time |
| time()+900 | 15 minutes from the current time |
| time()+1800 | 30 minutes from the current time |
| time()+3600 | One hour from the current time |
| time()+14400 | Four hours from the current time |
| time()+43200 | 12 hours from the current time |
| time()+86400 | 24 hours from the current time |
| time()+259200 | Three days from the current time |
| time()+604800 | One week from the current time |
| time()+2592000 | 30 days from the current time |
The goal of this little script is just to set a test cookie and then print a message to the screen. Before you start, ensure that you do not have any personal firewall settings blocking incoming cookies. Also, modify your web browser preferences to prompt you before setting cookies. This is the only way to watch a cookie as the server attempts to send it to your browser.
1. Open a new file in your text editor and start a PHP block. Then create a set of variables called $cookie_name, $cookie_value, $cookie_expire, and $cookie_domain, and give them the following values:
| PHP Code |
|---|
$cookie_name = "test_cookie"; $cookie_value = "test string!"; $cookie_expire = time()+86400; $cookie_domain = "127.0.0.1"; |
| Select all |
Note: Substitute your own domain name for the value of $cookie_domain, if you are not using 127.0.0.1 (localhost) as your domain.
2. Use the setcookie() function to set this test cookie and then close the PHP block:
| PHP Code |
|---|
setcookie($cookie_name, $cookie_value, $cookie_expire, "/" , $cookie_domain, 0); |
| Select all |
3. Type the following HTML:
| HTML Code |
|---|
<HTML> <HEAD> <TITLE>Set Test Cookie</TITLE> </HEAD> <BODY> <h1>cookie!</h1> </BODY> </HTML> |
| Select all |
4. Save the file with the name setcookie.php, and place this file in the document root of your web server.
5. Open your web browser and type http://127.0.0.1/setcookie.php.
You should see a dialog box prompting you to accept the cookie. The actual dialog box will differ from browser to browser, as will the action buttons.
Click on Allow to accept the cookie. Your outpus should be: "cookie!".
Nobody posted any comments regarding this story. Be the first!
Discuss