<?
$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;?> <button> Вторая страница →</button>
</body>
</html>
<?
$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;?> <button>← Первая страница </button>
</body>
</html>
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; }
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 '← Первая страница ':
document.location.href = window.location.protocol+'//'+window.location.host+window.location.pathname.replace(/[^\/]+$/, '');
break;
case ' Вторая страница →':
document.location.href = window.location.protocol+'//'+window.location.host+window.location.pathname+'second.phtml';
break;
}
};
};
xhr.onload = function()
{
window.location.reload(true);
};
<?
$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;
?>
<?
$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;
?>