PHP code for cookies - cookie can be defined by php variable $_COOKIE
PHP code for cookies
Example :
<?php
if(isset($_COOKIE['nameofcookie'])){
echo '<br>User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
echo '<br>User is not set<br>';
}
?>
====================
Version 2 for cookies
====================
<?php
//to change cookie
setcookie('nameofcookie','Frank', time() + (86400 *30));//set for a day
if(isset($_COOKIE['nameofcookie'])){
echo '<br>User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
echo '<br>User is not set<br>';
}
?>
=======================
Version 3 for cookies
=======================
<?php
//to change cookie
setcookie('nameofcookie','Frank', time() + (86400 *30));//set for a day
//to unset a cookie just set the time that is already past
//delete cookie
setcookie('nameofcookie','Frank', time() -3600);
if(isset($_COOKIE['nameofcookie'])){
echo '<br>User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
echo '<br>User is not set<br>';
}
?>
=========================
Version 4 check for cookies
=========================
<?php
//to change cookie
setcookie('nameofcookie','Frank', time() + (86400 *30));//set for a day
if(count($_COOKIE) > 0){
echo '<br>There are ' . count($_COOKIE) . ' cookies saved<br>';
}else{
echo '<br>There are no cookies saved<br>';
}
if(isset($_COOKIE['nameofcookie'])){
echo '<br>User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
echo '<br>User is not set<br>';
}
?>
Output :
Comments