Answers for "cookies and session in php"

PHP
5

destroy session php

<?php 
 session_start(); // start session
 session_destroy();  // Delete whole session
// OR
unset($_SESSION['username']); // delete any specific session only
?>
Posted by: Guest on June-15-2020
12

php cookies

//Cookies
//Cookies are stored on the client side. cookies are not as secure as sessions
//and it is recommended that you use sessions as much as possible.
====================
Version 1 for cookies
====================

<?php
if(isset($_COOKIE['nameofcookie'])){ 
    echo 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
    echo'User is not set';
}


====================
Version 2 for cookies
====================
<?php
    //to change cookie
    setcookie('nameofcookie','Frank', time() + (86400 *30));//set for a day

if(isset($_COOKIE['nameofcookie'])){ 
    echo 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
    echo'User is not set';
}

=======================
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 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
    echo'User is not set';
}

=========================
Version 4 check for cookies
=========================
<?php
    //to change cookie
    setcookie('nameofcookie','Frank', time() + (86400 *30));//set for a day

    if(count($_COOKIE) > 0){
        echo 'There are ' . count($_COOKIE)  .  ' cookies saved<br>';
        }else{
            echo 'There are no cookies saved<br>';
        }

if(isset($_COOKIE['nameofcookie'])){ 
    echo 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
    echo'User is not set';
}
Posted by: Guest on May-13-2020
3

create session in php

<?php 
  // Start the session
  session_start();

  // Set session variables
  $_SESSION["color"]= "blue";
  $_SESSION["animal"]= "dog";
  echo "The session variable are set up.";
?>
Posted by: Guest on June-16-2020
3

cookies php syntax

setcookie("cookie_name", "type_on_cookie", expiry_time(), "/");
Posted by: Guest on July-29-2020

Code answers related to "cookies and session in php"

Browse Popular Code Answers by Language