php

Notes and Practical Projects for learning php while my learning journey

View on GitHub

Cookies

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.


setcookie('cookie_name', 'cookie_value', 'expiration_date')
setcookie('cookie_name', 'cookie_value', 'expire_date', 'path', 'domain', 'secure', 'httponly_');
num_of_weeks = 2;
$expiration = time() + (60 * 60 * 24 * 7) * $num_of_weeks; 
// time() returns the current time 

// setcookie('key', 'value', 'expiration_date')
setcookie('username', 'mohamed', $expiration);
echo $_COOKIE['cookie_name'];
if(isset($_COOKIE['username']))
{
	echo $_COOKIE['username'];
}
else{
	echo 'No 🍪 found';
}

So what is the session ?