Get and set cookie in PHP
In PHP, cookies can be set using the setcookie() function, which allows developers to specify the name, value, and other properties of the cookie. Setting a cookie in PHP can be a useful technique for implementing features like user authentication, shopping carts, and customized preferences. In this article, we will explore the basics of setting and using cookies in PHP, including how to create, retrieve, and delete them, as well as best practices for using cookies in web applications.
Get and set cookie in PHP
To get and set cookie in PHP:
- Use PHP's in-built function setcookie() to set the cookie in the browser.
- Use $_COOKIE to get the cookie. The cookies can also be found in $_REQUEST.
Code example
<?php
setcookie(name, value, expire, path, domain, secure, httponly);
setcookie("CookieName", "CookieValue", 2147483647, "/"); //SET COOKIE THAT EXPIRES IN 2038
print($_COOKIE["CookieName"]); //USER FOR GETTING COOKIE
?>
To set a cookie in PHP, you can use the setcookie() function. The syntax for this function is:
setcookie(name, value, expire, path, domain, secure, httponly);
Here's what each parameter does:
- name: The name of the cookie.
- value: The value of the cookie.
- expire: The expiration time of the cookie. This is a Unix timestamp, so you can use the time() function to get the current time and add the number of seconds you want the cookie to last.
- path: The path on the server where the cookie will be available.
- domain: The domain that the cookie is available to.
- secure: Indicates whether the cookie should only be transmitted over a secure HTTPS connection.
- httponly: Indicates whether the cookie should only be accessible through the HTTP protocol and not through client-side scripts.
Set a Cookie in PHP
Here's an example that sets a cookie named "username" with a value of "John Doe" that will expire in 1 hour:
<?php
setcookie("username", "John Doe", time()+3600, "/", "", 0, 1);
?>
Note that the setcookie() function should be called before any output is sent to the browser, or it won't work.
Get a Cookie in PHP
To retrieve a cookie in PHP, you can use the $_COOKIE superglobal array. This array contains all the cookies that have been sent to the server from the client's browser. The syntax for accessing a cookie in this array is:
$_COOKIE['cookie_name'];
Here's an example that retrieves the value of a cookie named "username" and assigns it to a variable:
$username = $_COOKIE['username'];
Note that you should always check whether a cookie exists before trying to access it, to avoid errors. You can do this using the isset() function, like this:
if(isset($_COOKIE['username'])) {
$username = $_COOKIE['username'];
} else {
// the cookie does not exist
}
Once you have retrieved a cookie, you can use its value in your PHP code to perform various tasks, such as customizing the content of a web page or validating user credentials. However, you should be careful not to rely too heavily on cookies for sensitive data, as they can be manipulated by users and are not completely secure.