### Usage of PHP's `setcookie` Function
**Tip:** Sends cookie information to the browser.
**Syntax:**
`int setcookie(string name, string value, int expire, string path, string domain, int secure)`
**Return Value:** Integer
**Function Type:** Network System
**Description:**
This function sends cookie information to the browser as part of the HTTP header.
To create and modify a cookie, you can use the PHP function `setcookie()`. Depending on how much control you want over the cookie and who can read its value, `setcookie()` can take up to six parameters.
The simplest way to set a cookie is as follows:
```php
setcookie('name', 'bret');
```
Then, for every page viewed by the user with that browser until they log out, there will be a variable `$name` with the value "bret", which can easily be accessed via PHP. Since its lifetime is tied to a single user session, such cookies are called **session cookies**.
If you want the cookie to persist even after the user closes their browser, you must pass a third parameter to the `setcookie()` function, setting the expiration date for the cookie. Because PHP is rooted entirely in Unix philosophy, this expiration date must be represented as the total number of seconds since January 1, 1970. If you're a Unix programmer, this might make perfect sense to you. However, if you come from the Windows or Macintosh world, you may only shake your head in disbelief at those quirky Unix folks.
Fear not! PHP provides a very handy function called `mktime()`. You just need to pass the desired hour, minute, second, month, day, and year to `mktime()`, and it will return the total number of seconds since January 1, 1970. So, if you want to simulate the Y2K issue:
```php
$y2k = mktime(0, 0, 0, 1, 1, 2000);
setcookie('name', 'bret', $y2k);
```
Now, your cookie will expire in the year 2000.
If you need to update the cookie to store a new value, simply overwrite its old value. Therefore, even if you've already sent the cookie in a previous page, you can change your name to "jeff":
```php
$y2k = mktime(0, 0, 0, 1, 1, 2000);
setcookie('name', 'jeff', $y2k);
```
Note that doing this will not change the value of the variable `$name`. Its value is determined when the page loads. If you want to ensure both are always synchronized, you can write the following code:
```php
$name = 'jeff';
$y2k = mktime(0, 0, 0, 1, 1, 2000);
setcookie('name', $name, $y2k);
```
The next two parameters of `setcookie()` can control the domain and directory path where the cookie can be accessed. By default, the cookie is only readable by pages in the same or lower directory structure on the server that sent the cookie. This is a security measure. However, if you have an account on "www.domain.com" but also on "other.domain.com", and your account allows processing pages from ~/myhome, you should modify `setcookie()` as follows:
```php
setcookie('name', 'jeff', $y2k, '~/myhome', '.domain.com');
```
The last parameter of `setcookie()` that we haven't used yet sets the cookie to be sent only over secure connections like SSL to the web server. To use this feature, set the sixth value to 1.
Deleting a cookie is quite simple; just send the cookie's name to `setcookie()`, and PHP will delete it:
```php
setcookie('name');
```
Finally, there's one important point about using cookies. Since cookies work in a specific way with HTTP, you must send all cookies before outputting any text. Otherwise, PHP will issue a warning, and the cookies won't be sent. Here's the correct method:
```php
setcookie('name', 'jeff');
echo "Hello Everyone!";
```
Here’s the incorrect method:
```php
echo "Hello Everyone!";
setcookie('name', 'jeff');
```