Главная страница  /  Занятия  /  #15  /  Установка, чтение и удаление cookie в PHP

Установка, чтение и удаление cookie в PHP

Задание:

Рассказать об установке, чтении и удалении cookie в PHP.

phtml-документы:

Файл index.phtml:

   <?
    $cookieName = 'cookie-example';
    if(isset($_COOKIE[$cookieName]))
    {
     $cookieState = 'Cookie установлены. Значение cookie: '.$_COOKIE[$cookieName].'.';
$cookieButton = '<button>Удалить cookie</button>'; } else { $cookieState = 'Cookie не установлены.'; $cookieButton = '<button>Установить cookie</button>'; } ?> <!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Первая страница</title> <link rel="stylesheet" href="styles.css"> <script src="scripts.js"></script> </head> <body> <h1>Первая страница</h1> <p><?echo $cookieState;?></p> <?echo $cookieButton;?> &nbsp;&nbsp;<button>&nbsp;Вторая страница &nbsp;&rarr;</button> </body> </html>

Файл second.phtml:

   <?
    $cookieName = 'cookie-example';
    if(isset($_COOKIE[$cookieName]))
    {
     $cookieState = 'Cookie установлены. Значение cookie: '.$_COOKIE[$cookieName].'.';
$cookieButton = '<button>Удалить cookie</button>'; } else { $cookieState = 'Cookie не установлены.'; $cookieButton = '<button>Установить cookie</button>'; } ?> <!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Вторая страница</title> <link rel="stylesheet" href="styles.css"> <script src="scripts.js"></script> </head> <body> <h1>Вторая страница</h1> <p><?echo $cookieState;?></p> <?echo $cookieButton;?> &nbsp;&nbsp;<button>&larr;&nbsp; Первая страница&nbsp;</button> </body> </html>

CSS-разметка (styles.css):

   body		{ font-family: Arial, Tahoma, Verdana; font-size: 16px; }
   h1		{ background-color: #f0f0f0; padding: 16px; margin: 0px; }
   p		{ margin: 0; border: 1px solid #f0f0f0; padding: 16px; }
   button	{ width: 190px; height: 36px; font-size: 16px; background-color: #f0f0f0; border: 1px solid #d0d0d0; margin-top: 18px; outline: none; cursor: pointer; }

JS-скрипты (scripts.js):

   var xhr = new XMLHttpRequest();
   
   window.onload = function()
   {
    document.getElementsByTagName('button')[0].onclick = function()
    {
     switch(this.innerHTML)
     {
      case 'Установить cookie':
       xhr.open("GET", 'setcookie.php', true);
       xhr.responseType = 'text';
       xhr.send();
       break;
   
      case 'Удалить cookie':
       xhr.open("GET", 'deletecookie.php', true);
       xhr.responseType = 'text';
       xhr.send();
       break;
     }
    };
   
    document.getElementsByTagName('button')[1].onclick = function()
    {
     switch(this.innerHTML)
     {
      case '←&nbsp; Первая страница&nbsp;':
       document.location.href = window.location.protocol+'//'+window.location.host+window.location.pathname.replace(/[^\/]+$/, '');
       break;
   
      case '&nbsp;Вторая страница &nbsp;→':
       document.location.href = window.location.protocol+'//'+window.location.host+window.location.pathname+'second.phtml';
       break;
     }
    };
   };
   
   xhr.onload = function()
   {
    window.location.reload(true);
   };

PHP-скрипты:

Файл setcookie.php:

   <?
    $cookieDomain = $_SERVER['HTTP_HOST'];
    $cookieName = 'cookie-example';
    $cookieValue = 'education';
    $cookiePath = preg_replace('/[^\/]+$/', '', $_SERVER['REQUEST_URI']);
    $cookieSecure = true;
   
    setcookie($cookieName, $cookieValue, 0, $cookiePath, $cookieDomain, $cookieSecure);
header('Content-Type: text/html; charset=utf-8'); return; ?>

Файл deletecookie.php:

   <?
    $cookieDomain = $_SERVER['HTTP_HOST'];
    $cookieName = 'cookie-example';
    $cookieValue = 'education';
    $cookiePath = preg_replace('/[^\/]+$/', '', $_SERVER['REQUEST_URI']);
    $cookieSecure = true;
   
    setcookie($cookieName, $cookieValue, time()-3600, $cookiePath, $cookieDomain, $cookieSecure);
header('Content-Type: text/html; charset=utf-8'); return; ?>

Результат:

Результат выполнения задания можно посмотреть здесь.