- This topic has 0 replies, 1 voice, and was last updated 1 year, 1 month ago by
Chinomnso.
- AuthorPosts
- December 2, 2019 at 11:41 am #81424Participant@chinomnso
A cookie is a small piece of data sent by a website from a web server to a web browser to be stored there as a way to remember pieces of information about a user’s browsing activity. In other words, a cookie is a mechanism for storing data in a browser and using that data to track or identify returning users.
When can you use cookies?
They can be used when you want to track what pages a user has visited on your site, or to enhance user experience. As an example of tracking users, consider this: You’re adding a voting feature to a page on your site. Let us assume users are not allowed to create accounts on your site, so you have to find a way to track users who have previously voted and prevent them from voting again.
Once a user votes, you have to create a cookie and save it in the user’s browser. When the user returns to the site, you can disable the voting section on the webpage, or redirect them to another page if the voting page was created exclusively for that purpose.
More about cookies
Generally, a cookie binds small pieces of data to a specific user. A cookie contains at least, two bits of data: the name of the cookie and a value. But a cookie may also be made up of much more information like its scope, validity, and even more.
Apart from cookies that are stored in your browser by the website you are directly visiting, other cookies can be stored in your browser by other sites linked to the one you’re visiting. These are third-party cookies. Perhaps, the most annoying application of third-party cookies is their usage in advertising. You search for home audio systems on an e-commerce site, and for the next two weeks, every single site you visit shows you home audio ads. Third-party cookies are to blame for all of this.
How to implement cookies in PHP
PHP supports the implementation of cookies out-of-the-box. Therefore, you don’t need to install any extensions before you can start working with cookies. Cookies can be set using
setcookie()
orsetrawcookie()
function. Because cookies are part of the HTTP header,setcookie()
must be called before any output is sent to the browser.Here’s the basic syntax for implementing cookies in PHP:
1setcookie ( string $name [, string $value = "" [, array $options = [] ]] ) : boolThe
setcookie()
function has the following parameters:name
The name of the cookie you’re setting.value
The value assigned to the cookie. Because the value of the cookie is saved on a user’s computer, be sure that you’re not storing sensitive information with the cookie.expires
This sets the validity of the cookie by assigning it an expiration date. This is a Unix timestamp. Therefore you’ll probably set it using thetime()
function and append the number of seconds till the expiration. Here’s an example:1$expires = time()+60*60*24*60;The code above gets the current time and adds it to the total number of seconds in 60 days and assigns the result of the calculation to a variable. This means the cookie would end in 60 days. If you set the expiration time to 0, the cookie expires at the end of the browsing session, or when the browser closes.
To access cookies in PHP,
$_COOKIE['cookiename']
is used. You may also be able to access the value of the cookie via$_REQUEST['cookiename']
.Remember though, that a user can circumvent cookie-based restrictions by disabling cookies in their browser, or by clearing their browser cookies. So, there are certain features of your site that you certainly would not use cookies for.
To learn more about the
setcookie()
function, check out the official documentation on php.net. - AuthorPosts
- You must be logged in to reply to this topic.