Cookies are pieces of text that are sent to a user’s Web browser. Cookies can help you create shopping carts, user communities, and personalized sites. It’s not recommended that you store sensitive data in a cookie, but you can store a unique identification string that will match a user with data held securely in a database.
The setcookie() function is the one we will be using to create cookies.
setcookie(“test”, “ok”, “”, “/”, “localhost”, 0);
The setcookie() function, used to set one cookie at a time, expects six arguments:
- Name. Holds the name of the variable that is kept in the global $_COOKIE and is accessible in subsequent scripts.
- Value. The value of the variable passed in the name parameter.
- Expiration. Sets a specific time at which the cookie value is no longer accessible. Cookies without a specific expiration time expire when the Web browser closes.
- Path. Determines for which directories the cookie is valid. If a single slash is in the path parameter, the cookie is valid for all files and directories on the Web server. If a specific directory is named, this cookie is valid only for pages within that directory.
- Domain. Cookies are valid only for the host and domain that set them. If no domain is specified, the default value is the hostname of the server that generated the cookie. The domain parameter must have at least two periods in the string in order to be valid.
- Security. If the security parameter is 1, the cookie will only be transmitted via HTTPS, which is to say, over a secure Web server.
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