The cookie is used to store some information in the user’s computer by the web browser when the user visits a website. When a user sends a request for a web page on a website using a browser, the server sends the set-cookie HTTP headers with the response that will request the browser to create the cookie file in the user’s computer. If the cookie is disabled in the browser, no cookie information will be stored
in the user’s computer. The cookie information can be accessed in PHP by using a PHP superglobal variable named $_COOKIE. The purpose of using cookies and the ways to set cookie data using PHP script are shown in this tutorial. The setcookie() function is used to define the cookie that will be sent with the HTTP header. The syntax of this function is provided below: The first argument contains the cookie name. The second argument contains the cookie
value. The third argument contains an array of cookie options. The values of the array can contain the values of expires, path, domain, secure, and httponly. The expires contains the time limit for the existing cookie. The path defines the server path where the cookie information will be stored. The domain contains the value where the cookie will be available. The secure indicates the cookie information
will be transmitted over the HTTPS connection. The value of the httponly is set to true to confirm that the cookie data will be accessible through the HTTP protocol. The options associative array can contain one or more values mentioned here. The $_COOKIE variable is used to read the cookie values. It returns true upon success and returns false upon failure. Different uses of the cookie have been shown in this part of the tutorial by using multiple examples: Example 1: Set a Simple Cookie Value <?php //Check the submit button is clicked or not <form action = "#" method = "get"> <?php Output: The following output will appear if the user refreshes the page after submitting the value “fahmida” by using the form: Example
2: Set a Simple Cookie With Expire Date <?php //Check the submit button is clicked or not <form action="#" method="get"> Output: The following output will appear if the user refreshes the page after submitting the value, “Fahmida Yesmin” by using the form: Example 3: Delete Existing Cookie Values <?php Output: ConclusionThe purpose of using the cookie and the ways to create, read, and delete the cookie in PHP have been explained in this tutorial by using different examples. I hope, the PHP users will be able to use the cookie in PHP properly after reading this tutorial. Check out Linux Hint for more tips and information. About the authorI am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help. Where do cookies get stored?If you're wondering “where are cookies stored,” it's simple: your web browser will store it locally to remember the “name-value pair” that identifies you. If a user returns to that site in the future, the web browser returns that data to the web server in the form of a cookie.
What are cookies how they are created and stored in PHP?A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
How cookie is used in PHP?PHP cookie is a small piece of information which is stored at client browser. It is used to recognize the user. Cookie is created at server side and saved to client browser. Each time when client sends request to the server, cookie is embedded with request.
How can I tell if PHP cookies are created?PHP Cookies Checking if a Cookie is Set
Use the isset() function upon the superglobal $_COOKIE variable to check if a cookie is set.
|