Hi,
I was searching for the code to set a cookie at the start of the website like you can see at
http://www.apniisp.com , they have given the cookie at the start of the website which automatically popups.
plz send me as soon as possible

Dani AI

Generated

Brief summary and recommendation: the cleanest, most reliable pattern is to set a server-side cookie at the very start of the request and then render an in-page modal or lightbox only when that cookie is absent. That follows the intent of the thread (see ), acknowledges 's pointer to cookie docs, and offers an alternative to 's popup-window approach (window.open is frequently blocked and provides a poorer UX).

Core server-side check (place this before any HTML output). This example sets a 30-day flag and uses the modern options array when available:

<?php
$cn = 'seen_popup';
$expiry = time() + 30*24*60*60; // 30 days
if (empty($_COOKIE[$cn])) {
    if (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 70300) {
        setcookie($cn, '1', ['expires'=>$expiry, 'path'=>'/', 'secure'=>(!empty($_SERVER['HTTPS'])), 'httponly'=>true, 'samesite'=>'Lax']);
    } else {
        setcookie($cn, '1', $expiry, '/', '', (!empty($_SERVER['HTTPS'])), true);
    }
    $show_popup = true;
} else {
    $show_popup = false;
}
?>

Render a hidden modal and reveal it client-side on DOM ready (avoids popup blockers and is mobile friendly):

<?php if ($show_popup): ?>
<div id="site-popup" style="display:none">
  <div class="popup-inner">Welcome message or promo content here.</div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function(){
  var p = document.getElementById('site-popup');
  if (p) p.style.display = 'block';
});
</script>
<?php endif; ?>

Troubleshooting and notes: setcookie must run before any output (or use output buffering). window.open will often be blocked — prefer an in-page modal for reliability. For site-wide checks done on the server, cookies are preferable; for purely client-only logic localStorage is an alternative. Include Secure/HttpOnly/SameSite options for modern browsers and be mindful of privacy/regulatory requirements when showing tracking or promotional content.

Recommended Answers

All 4 Replies

but what about that pop-up window

I want it very urgently because

try this javascript function which will be called with php code

add this code in your inside the head tag but below the title tag

<script type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</script>

and this code you will add it inside the body tag, but you have to first test if the cookie is not set. if not set thats when you put this code

<?php   echo"<SCRIPT LANGUAGE='JavaScript'> MM_openBrWindow('yourpage.html','advert','status=yes,resizable=yes,width=500,height=400')</SCRIPT>";

where it says:

your page- put the name of the page you want to be popped
advert- just the name you want to give it
status- is if you want it to show status bar
resizable- is when you want it to be resizable
width and height- thats the size of the pop up

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.