Create a pop-up message We use cookies easily and for free!
This script does the following:
Set the lifetime of the form yourself.
The popup is centered and pinned to the bottom of the screen.
Please select one of the options.
<?php
$cookie_name = "welcome_closed";
$cookie_value = "closed";
$expiration = time() + (86400 * 30);
if(!isset($_COOKIE[$cookie_name])) {
?>
<div id="welcome-message">
We use cookies.
<button id="close-welcome">X</button>
</div>
<style>
#welcome-message {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
width: 80%;
max-width: 600px;
background-color: #f0f0f0;
padding: 15px;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
z-index: 9999;
box-sizing: border-box;
text-align: center;
}
#close-welcome {
position: absolute;
top: 10px;
right: 5px;
cursor: pointer;
background-color: #00AF92;
border: none;
padding: 5px 10px;
border-radius: 3px;
}
</style>
<script>
document.getElementById('close-welcome').addEventListener('click', function() {
document.getElementById('welcome-message').style.display = 'none';
document.cookie = "<?php echo $cookie_name; ?>=<?php echo $cookie_value; ?>; max-age=<?php echo $expiration - time(); ?>; path=/";
});
</script>
<?php
}
?>
<?php
$cookie_name = "welcome_closed";
$cookie_value = "closed";
$expiration = time() + (86400 * 365);
if(!isset($_COOKIE[$cookie_name])) {
?>
<div id="welcome-message">
We use cookies.
<button id="close-welcome">X</button>
</div>
<style>
#welcome-message {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
width: 80%;
max-width: 600px;
background-color: #f0f0f0;
padding: 15px;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
z-index: 9999;
box-sizing: border-box;
text-align: center;
}
#close-welcome {
position: absolute;
top: 10px;
right: 5px;
cursor: pointer;
background-color: #00AF92;
border: none;
padding: 5px 10px;
border-radius: 3px;
}
</style>
<script>
document.getElementById('close-welcome').addEventListener('click', function() {
document.getElementById('welcome-message').style.display = 'none';
document.cookie = "<?php echo $cookie_name; ?>=<?php echo $cookie_value; ?>; max-age=<?php echo $expiration - time(); ?>; path=/";
});
</script>
<?php
}
?>
Install
Wordpress: in the root of the site, open the index.php file and paste the copied code from the first line. The rest of the code that is in this site should be after the new code. See how it should be.
Others: The code should be inserted at the very beginning of your php file.
Change the pop-up text as desired.
Result