php

Cookies


 

In PHP, cookies are small pieces of data sent from a server to a user's browser and stored on the user's device. Cookies are commonly used for various purposes, such as session management, user preferences, and tracking.

 

PHP setcookie():

The `setcookie()` function is used to set a cookie in PHP.

 

Syntax: 

<?php
		setcookie(name, value, expire, path);
	?>

 

Example:

<?php
		//set a cookie
		setcookie("username", "Raj", time()+1800, "codersmile.com");
	?>

This example sets a cookie named "username" with the value "Raj," valid for half hour, accessible across the entire domain.

 

PHP $_COOKIE

The $_COOKIE superglobal variable used to get cookie in PHP.

 

Example:

<?php
		$value = $_COOKIE["name"];
	?>

 

PHP Deleting a Cookie

If you set the expiration time, then cookie automatically deleted.

 

Example:

<?php
		//cookie will be deleted after half hour
		setcookie("username", "raj", time()-1800);
	?>