Cookies management in PHP

Cookies management in PHP

Cookies are small text files that are stored on the user’s computer by the browser. Cookies are commonly used in web applications to store user preferences, login information, and other data that needs to persist between requests.

In PHP, you can manage cookies using the setcookie() function. The setcookie() function accepts up to seven parameters:

  • name: the name of the cookie.
  • value: the value of the cookie.
  • expires: the expiration time of the cookie in Unix timestamp format. If not set, the cookie will expire when the browser is closed.
  • path: the path on the server where the cookie will be available.
  • domain: the domain where the cookie will be available.
  • secure: indicates whether the cookie should only be transmitted over a secure HTTPS connection.
  • httponly: indicates whether the cookie should only be accessible through HTTP requests, and not through client-side scripting languages like JavaScript.

Here’s an example of setting a cookie in PHP:

// Set a cookie with the name "username" and value "john"
setcookie("username", "john", time() + 3600, "/");

// Retrieve the value of the "username" cookie
echo $_COOKIE["username"];

Apply for PHP Certification!

https://www.vskills.in/certification/certified-php-developer

Back to Tutorials

Share this post
[social_warfare]
Mysql error handling
Design ListView and Other Templated Controls

Get industry recognized certification – Contact us

keyboard_arrow_up