View content based on if a new session is created I am running a Drupal 7 website using the Ubercart module. I am trying to show some HTML content when users get on the site, and not keep viewing it when they click on a certain button. I need this piece of content to appear at the top of the website so I tried putting it in the HTML in the page.tpl.php file, which works, but then it keeps appearing every time the user goes to a different page or reloads the page. The way I am thinking of approaching this is as follows:

1) A new session is created when a user lands on the site.

2) If a new session is created, I will copy that session's value into another variable ($variable = session_id();), and execute some code when the content of $variable is equal to the value returned from session_id(). 

3) I will have that piece of content as a &lt;form method="POST"&gt;. When the user clicks on the close button, that will $variable = $variable + 1;.

4) This way the if statement will be FALSE for this session and the content won't appear anymore.

my problem is with knowing if a NEW session has been created or not. I have tried using if (drupal_session_started()) { echo "s";} ?&gt; but that doesn't print anything which means I still can not check for new sessions started. Shortly, I need a way to know if a new session has been started on the site, so I can do the mentioned steps. Bonus if you tell me what you think about how I'm going about doing this, because I'm a Drupal newbie and there might be easier solutions.
On Drupal, when a new user logs in, a new session is started; when the session expires, the user must log in again. You just need to use the hook that Drupal uses when a user logs in, which is hook_user_login(). Take system_user_login() as code example.

function system_user_login(&amp;$edit, $account) {
  // If the user has a NULL time zone, notify them to set a time zone.
  if (!$account-&gt;timezone &amp;&amp; variable_get('configurable_timezones', 1) &amp;&amp; variable_get('empty_timezone_message', 0)) {
    drupal_set_message(t('Configure your &lt;a href="@user-edit"&gt;account time zone setting&lt;/a&gt;.', array('@user-edit' =&gt; url("user/$account-&gt;uid/edit", array('query' =&gt; drupal_get_destination(), 'fragment' =&gt; 'edit-timezone')))));
  }
}


Keep in mind that PHP variables don't persist between sessions. If, for example, you need to show the content only once for users, you need to use code similar to the following one.

function mymodule_user_login(&amp;$edit, $account) {
  if (variable_get("mymodule_show_message_{$account-&gt;uid}", 1)) {
    // Show the message.
    variable_set("mymodule_show_message_{$account-&gt;uid}", 0);
  }
}


For anonymous users, you cannot count on the session being saved between two different page requests. Drupal, at the end of a page request, destroys the session for anonymous users, if it is empty. See drupal_session_commit().

  if (empty($user-&gt;uid) &amp;&amp; empty($_SESSION)) {
    // There is no session data to store, destroy the session if it was
    // previously started.
    if (drupal_session_started()) {
      session_destroy();
    }
  } 


For a solution that works for both the anonymous users and authenticated users, I would rather implement hook_page_build(), although this has the side effect of always creating the session for anonymous users, with all the consequences.

function mymodule_page_build(&amp;$page) {
  if (empty($_SESSION['mymodule_page_disclaimer_shown'])) {
    // This is a new session. Show the disclaimer.
    $page['page_top']['disclaimer'] = array(
      '#markup' =&gt; t('Acme, Inc. is not responsible for the contents of this sample code.'),
      '#weight' =&gt; 0,
    );
    $_SESSION['mymodule_page_disclaimer_shown'] = TRUE;
  }
}


In this way, since the session is not empty, it won't be destroyed from Drupal after serving the page. The second time the anonymous users visit the site using the same computer/browser, the disclaimer will not be shown.