Dynamic Add Section

PHP - Cookies

Dynamic Add Section

In PHP, cookies are small pieces of data that can be stored on the client-side (in the user's browser) and sent back to the server with subsequent requests. Cookies are commonly used to store user preferences, session information, or tracking data. Here's how you can work with cookies in PHP:

1. Setting a Cookie:

In this example, the setcookie() function is used to set a cookie named "username" with the value "John" and an expiration time of 30 days. The cookie is sent to the client's browser in the response headers.

2. Accessing a Cookie:

Here, the $_COOKIE superglobal is used to access the value of the "username" cookie. If the cookie is set, the script retrieves the value and displays a personalized message. If the cookie is not found, it displays a default message.

3. Deleting a Cookie:

To delete a cookie, you can set the expiration time to a past time using setcookie(). In this example, the "username" cookie is deleted by setting the expiration time to one hour ago. The browser will remove the cookie from the client-side storage.


It's important to note that cookies are stored on the client-side and can be modified or deleted by the user. Therefore, you should avoid storing sensitive information in cookies and implement server-side validation and security measures when using cookies.

Dynamic Add Section
Dynamic Add Section
Dynamic Add Section
Dynamic Add Section
Dynamic Add Section
Dynamic Add Section