Setup simple cookies in jQuery
(function($){
$(document).ready(function(){
// Cookie Setup
var cookieStorage = {
setCookie: function setCookie(key, value, time, path) {
var expires = new Date();
expires.setTime(expires.getTime() + time);
var pathValue = '';
if (typeof path !== 'undefined') {
pathValue = 'path=' + path + ';';
}
document.cookie = key + '=' + value + ';' + pathValue + 'expires=' + expires.toUTCString();
},
getCookie: function getCookie(key) {
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
return keyValue ? keyValue[2] : null;
},
removeCookie: function removeCookie(key) {
document.cookie = key + '=; Max-Age=0; path=/';
}
};
$('.button-class').on('click', function() {
// check Cookie status
var status = cookieStorage.getCookie('cookie-name');
if ( status == 'cookie-status' ) {
// do something
} else {
// do something else
}
// remove cookie
cookieStorage.removeCookie('cookie-name');
// set cookie - 1 day cookie (1 * 86400000 (24 * 60 * 60 * 1000))
// name, status, duration, path (use / for all)
cookieStorage.setCookie('cookie-name', 'cookie-status', 86400000, '/');
});
});
})(jQuery);
How to check cookies in PHP
<?php
// check cookie status in PHP
$cookie_status = isset( $_COOKIE['cookie-name'] ) ? $_COOKIE['cookie-name'] : '';
if ( $cookie_status == 'cookie-status' ) {
// do something
}