Wrapping javascript code conditionally using a cookie I want to include the following code in page.tpl.php only once during first site visit and for subsequently not. I mean this will pop-up an ad only once. I want to set it based on a cookie. How to conditionally include this code based on a cookie? I need the sample code.

&lt;script language="JavaScript"&gt;
        var zflag_nid="1185"; var zflag_cid="3307"; var zflag_sid="823"; var zflag_width="1"; var zflag_height="1"; var zflag_sz="15";
        &lt;/script&gt;
        &lt;script language="JavaScript" src="http://d8.zedo.com/jsc/d8/fo.js"&gt;&lt;/script&gt;


EDIT:

Will this work in page.tpl.php ? I want add it before &lt;/head> so that the js file is included only once in user session.

&lt;script type="text/javascript"&gt;
    if(!jQuery.cookie('myCookie'))
    {
    $.cookie('myCookie':'myValue');
    &lt;!-- Javascript tag: --&gt;
&lt;script language="JavaScript"&gt;
var zflag_nid="1185"; var zflag_cid="3307"; var zflag_sid="823"; var zflag_width="1"; var zflag_height="1"; var zflag_sz="15";
&lt;/script&gt;
&lt;script language="JavaScript" src="http://d8.zedo.com/jsc/d8/fo.js"&gt;&lt;/script&gt;
    }

&lt;/script&gt;
This StackOverflow question should contain enough information to solve your issue.

I would go with the one that suggests using setcookie(). $_SESSION information is cleared when the user's session ends, generally at the close of the browser. Here is a recent example of me using cookies to remember an anonymous user's submission of a Webform and redirect them to the completion page if they tried to visit the Webform again:

    &lt;?php
    define('REQUEST_TIME', $_SERVER['REQUEST_TIME']);

    function _mymodule_manage_cookie($nid, $type = 'my_cookie_type') {
      $matches = (bool) $nid == variable_get("mymodule_{$type}_webform_nid", FALSE);
      if (empty($_COOKIE["Drupal_visitor_mymodule_{$type}_submitted"]) &amp;&amp; $matches) {
        mymodule_cookie_save(array("mymodule_{$type}_submitted" =&gt; TRUE));
      }
    }

    /**
     * Drupal 7 yoink!
     */
    function mymodule_cookie_save(array $values) {
      foreach ($values as $field =&gt; $value) {
        // Set cookie for 365 days.
        setrawcookie('Drupal.visitor.' . $field, rawurlencode($value), REQUEST_TIME + 31536000, '/');
      }
    }

    function mymodule_cookie_delete($cookie_name) {
      setrawcookie('Drupal.visitor.' . $cookie_name, '', REQUEST_TIME - 3600, '/');
    }
    ?&gt;


You need to put this in a module or in template.php in your theme. You can conditionally include the code in a manner similar to the following:

template.php

    &lt;?php
    function THEMENAME_preprocess_page(&amp;$variables) {
      // THEMENAME_get_first_visit_cookie() would refer to a function
      // you have to write that retrieves if the cookie
      // is set. You could alternatively try just using
      // $_COOKIE['my_cookie_type'].
      if (THEMENAME_get_first_visit_cookie()) {
        $variables['first_visit'] = TRUE;
      }
    }
    ?&gt;


page.tpl.php

    (skip to wherever you want to embed the code)
    &lt;?php if ($first_viist): ?&gt;
      // Paste your code here
    &lt;?php endif; ?&gt;


Now, one more point. The way I just showed you isn't the best way. You should add your Javascript properly using drupal_add_js() in a module or by adding a separate Javascript file in the theme that reacts to a class on the &lt;body&gt; tag and displays your first visit message (or whatever you're doing). You would still need the $first_visit variable to determine whether to add the class or not.