Skip to content

Instantly share code, notes, and snippets.

@dhaval-parekh
Last active September 25, 2023 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhaval-parekh/7ad5d4d5040c6e921256a57b750901ce to your computer and use it in GitHub Desktop.
Save dhaval-parekh/7ad5d4d5040c6e921256a57b750901ce to your computer and use it in GitHub Desktop.
Cookies Experiment Page
<?php
/**
* Cookies experiment.
*/
$expire_time = strtotime( '+30 days' );
$domain = $_SERVER['HTTP_HOST']; //parse_url( $_SERVER['HTTP_HOST'], PHP_URL_HOST );
$external_domain = 'example.com';
$cookies_data = [
[
'name' => 'simple',
'value' => 'Simple Cookie',
'expires' => $expire_time,
'path' => '/',
'domain' => $domain,
'secure' => false,
'httponly' => false,
'options' => [
'samesite' => 'None',
],
],
[
'name' => 'simple_secured',
'value' => 'Simple Cookie - Secured',
'expires' => $expire_time,
'path' => '/',
'domain' => $domain,
'secure' => true,
'httponly' => false,
'options' => [
'samesite' => 'None',
],
],
[
'name' => 'simple_httponly',
'value' => 'Simple Cookie - Http Only',
'expires' => $expire_time,
'path' => '/',
'domain' => $domain,
'secure' => false,
'httponly' => true,
'options' => [
'samesite' => 'None',
],
],
[
'name' => 'simple_secured_httponly',
'value' => 'Simple Cookie - Secured + Http Only',
'expires' => $expire_time,
'path' => '/',
'domain' => $domain,
'secure' => true,
'httponly' => true,
'options' => [
'samesite' => 'None',
],
],
// Internal Lax.
[
'name' => 'simple_lax',
'value' => 'Simple Cookie',
'expires' => $expire_time,
'path' => '/',
'domain' => $domain,
'secure' => false,
'httponly' => false,
'options' => [
'samesite' => 'Lax',
],
],
[
'name' => 'simple_secured_lax',
'value' => 'Simple Cookie - Secured',
'expires' => $expire_time,
'path' => '/',
'domain' => $domain,
'secure' => true,
'httponly' => false,
'options' => [
'samesite' => 'Lax',
],
],
[
'name' => 'simple_secured_httponly_lax',
'value' => 'Simple Cookie - Secured + Http Only',
'expires' => $expire_time,
'path' => '/',
'domain' => $domain,
'secure' => true,
'httponly' => true,
'options' => [
'samesite' => 'Lax',
],
],
// Internal Strict
[
'name' => 'simple_strict',
'value' => 'Simple Cookie',
'expires' => $expire_time,
'path' => '/',
'domain' => $domain,
'secure' => false,
'httponly' => false,
'options' => [
'samesite' => 'Strict',
],
],
[
'name' => 'simple_secured_strict',
'value' => 'Simple Cookie - Secured',
'expires' => $expire_time,
'path' => '/',
'domain' => $domain,
'secure' => true,
'httponly' => false,
'options' => [
'samesite' => 'Strict',
],
],
[
'name' => 'simple_secured_httponly_strict',
'value' => 'Simple Cookie - Secured + Http Only',
'expires' => $expire_time,
'path' => '/',
'domain' => $domain,
'secure' => true,
'httponly' => true,
'options' => [
'samesite' => 'Strict',
],
],
/**
* Third party Cookies
*/
[
'name' => 'external',
'value' => 'External Cookie',
'expires' => $expire_time,
'path' => '/',
'domain' => $external_domain,
'secure' => false,
'httponly' => false,
'options' => [
'samesite' => 'None',
],
],
[
'name' => 'external_secured',
'value' => 'External Cookie - Secured',
'expires' => $expire_time,
'path' => '/',
'domain' => $external_domain,
'secure' => true,
'httponly' => false,
'options' => [
'samesite' => 'None',
],
],
[
'name' => 'external_httponly',
'value' => 'External Cookie - Http Only',
'expires' => $expire_time,
'path' => '/',
'domain' => $external_domain,
'secure' => false,
'httponly' => true,
'options' => [
'samesite' => 'None',
],
],
[
'name' => 'external_secured_httponly',
'value' => 'External Cookie - Secured + HTTP Only',
'expires' => $expire_time,
'path' => '/',
'domain' => $external_domain,
'secure' => true,
'httponly' => true,
'options' => [
'samesite' => 'None',
],
],
// External Lax
[
'name' => 'external_lax',
'value' => 'External Cookie',
'expires' => $expire_time,
'path' => '/',
'domain' => $external_domain,
'secure' => false,
'httponly' => false,
'options' => [
'samesite' => 'Lax',
],
],
[
'name' => 'external_secured_lax',
'value' => 'External Cookie - Secured',
'expires' => $expire_time,
'path' => '/',
'domain' => $external_domain,
'secure' => true,
'httponly' => false,
'options' => [
'samesite' => 'Lax',
],
],
[
'name' => 'external_secured_httponly_lax',
'value' => 'External Cookie - Secured + HTTP Only',
'expires' => $expire_time,
'path' => '/',
'domain' => $external_domain,
'secure' => true,
'httponly' => true,
'options' => [
'samesite' => 'Lax',
],
],
// External Strict
[
'name' => 'external_strict',
'value' => 'External Cookie',
'expires' => $expire_time,
'path' => '/',
'domain' => $external_domain,
'secure' => false,
'httponly' => false,
'options' => [
'samesite' => 'Strict',
],
],
[
'name' => 'external_secured_strict',
'value' => 'External Cookie - Secured',
'expires' => $expire_time,
'path' => '/',
'domain' => $external_domain,
'secure' => true,
'httponly' => false,
'options' => [
'samesite' => 'Strict',
],
],
[
'name' => 'external_secured_httponly_strict',
'value' => 'External Cookie - Secured + HTTP Only',
'expires' => $expire_time,
'path' => '/',
'domain' => $external_domain,
'secure' => true,
'httponly' => true,
'options' => [
'samesite' => 'Strict',
],
],
];
foreach ( $cookies_data as $cookie ) {
setcookie(
$cookie['name'],
$cookie['value'],
[
'expires' => $cookie['expires'],
'path' => $cookie['path'],
'domain' => $cookie['domain'],
'secure' => $cookie['secure'],
'httponly' => $cookie['httponly'],
'samesite' => $cookie['options']['samesite'],
],
);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cookies Testing Site.</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="jumbotron text-center">
<h1>Cookies Experiment</h1>
<p><strong>Current Domain: </strong> <?php echo $domain; ?></p>
<p>
<!-- <button onclick="clearCookies()">Clear Cookies</button>-->
</p>
</div>
<div class="container">
<h2>Cookies accessible from server side.</h2>
<?php
print_r( PHP_EOL . '<pre style="position: relative;border: 1px solid #999;border-radius: 7px;padding: 10px;font-size: 12px;line-height: 1.2;background-color: #FFF;color: #090909;width: 100%;max-height: 512px;overflow: scroll;">' . print_r( $_COOKIE, 1 ) . '</pre>' . PHP_EOL );
?>
</div>
<div class="container">
<h2>Cookies set from server side.</h2>
<table id="cookies-table" class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Value</th>
<th scope="col">Domain</th>
<th scope="col">Http Only</th>
<th scope="col">Secure</th>
<th scope="col">Same Site</th>
<th scope="col">Accepted In JS</th>
</tr>
</thead>
<tbody>
<?php
foreach ( $cookies_data as $index => $cookie ) {
$count = $index + 1;
$name = $cookie['name'];
$httponly = $cookie['httponly'] ? 'Yes' : 'No';
$secure = $cookie['secure'] ? 'Yes' : 'No';
$samesite = $cookie['options']['samesite'] ? 'Yes' : 'No';
?>
<tr id="<?php echo $name; ?>">
<th scope="row"><?php echo $count; ?></th>
<td><?php echo $name; ?></td>
<td><?php echo $cookie['value']; ?></td>
<td><?php echo $cookie['domain']; ?></td>
<td>
<span class="text-<?php echo $cookie['httponly'] ? 'success' : 'danger'; ?>"><?php echo $httponly; ?></span>
</td>
<td>
<span class="text-<?php echo $cookie['secure'] ? 'success' : 'danger'; ?>"><?php echo $secure; ?></span>
</td>
<td>
<?php echo $cookie['options']['samesite']; ?>
</td>
<td class="text-center"><label><input id="check-<?php echo $name; ?>" class="cookies-checkbox"
type="checkbox"></label></td>
</tr>
<?php
}
?>
</tbody>
</table>
<div class="row">
<div class="col-md-6"><img src="https://media.cnn.com/api/v1/images/stellar/prod/230905135919-02-novaya-zemlya-nuclear-test-site.jpg?c=16x9&amp;q=h_438,w_780,c_fill" class="img-fluid" style="max-width: 100%; height: auto;"></div>
<div class="col-md-6"><iframe src="https://cookieexperiments.com/" style="width: 500px;height: 700px;"/></div>
</div>
</div>
</body>
<script type="application/javascript">
function getCookie( cookiename ) {
// Get name followed by anything except a semicolon
var cookiestring = RegExp( cookiename + "=[^;]+" ).exec( document.cookie );
// Return everything after the equal sign, or an empty string if the cookie name not found
return decodeURIComponent( !!cookiestring ? cookiestring.toString().replace( /^[^=]+./, "" ) : "" );
}
function clearCookies() {
// var cookies = document.cookie.split(";");
//
// for (var i = 0; i < cookies.length; i++) {
// var cookie = cookies[i];
// var eqPos = cookie.indexOf("=");
// var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
// document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
// }
document.cookie.split(";")
.forEach(function(c) {
document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });
alert( 'Cookies are Cleared...' );
updateElements();
}
function updateElements() {
var table = document.getElementById( 'cookies-table' );
var rows = table.querySelectorAll( 'tbody > tr' );
for ( var row of rows ) {
var name = row.getAttribute( 'id' );
if ( getCookie( name ) ) {
var radio = document.getElementById( 'check-' + name );
if ( radio ) {
radio.setAttribute( 'checked', 'checked' );
}
}
}
}
window.addEventListener( 'DOMContentLoaded', ( event ) => {
updateElements();
} );
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment