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 <form method="POST">. 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";} ?> 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.

2 Answers 2


There are some modules that do this:

Popup message (D7+D8)

Display popup message for users once per browser session.

Even if you don't like the way this module handles things, you can look at the code to see how it limits the message to once per session.

Splashify (D7+D8)

Display a splash page anywhere on your site by redirecting the user, showing a lightbox, or displaying a popup. This module is designed to be search engine friendly, mobile device friendly and offers multiple configuration options.

  • You're the real MVP bro. Splashify is awesome! Thank you!
    – Marwan AK
    Commented Dec 3, 2017 at 8:16
  • @MarwanAK Thanks! On Drupal Answers, if you find an answer useful, you should press the "up" arrow to increase its score. You can upvote multiple answers if multiple answers help you; there is no limit. For the answer that best solves your problem, click the checkbox to mark that answer as the "accepted" answer. Commented Dec 3, 2017 at 8:46

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(&$edit, $account) {
  // If the user has a NULL time zone, notify them to set a time zone.
  if (!$account->timezone && variable_get('configurable_timezones', 1) && variable_get('empty_timezone_message', 0)) {
    drupal_set_message(t('Configure your <a href="@user-edit">account time zone setting</a>.', array('@user-edit' => url("user/$account->uid/edit", array('query' => drupal_get_destination(), 'fragment' => '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(&$edit, $account) {
  if (variable_get("mymodule_show_message_{$account->uid}", 1)) {
    // Show the message.
    variable_set("mymodule_show_message_{$account->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->uid) && empty($_SESSION)) {
    // There is no session data to store, destroy the session if it was
    // previously started.
    if (drupal_session_started()) {

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(&$page) {
  if (empty($_SESSION['mymodule_page_disclaimer_shown'])) {
    // This is a new session. Show the disclaimer.
    $page['page_top']['disclaimer'] = array(
      '#markup' => t('Acme, Inc. is not responsible for the contents of this sample code.'),
      '#weight' => 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.

  • I'm looking to view the content for all visitors regardless if they're registered users or not. AKA for anonymous users too. I'm not sure what you mean by log in here, cause I'm thinking maybe you think I am looking for registered users logging in only. Thanks for replying
    – Marwan AK
    Commented Dec 3, 2017 at 8:18
  • With anonymous users, you cannot count on the session being stored between two page requests. Drupal destroys the current session for anonymous users, if it contains nothing.
    – apaderno
    Commented Dec 3, 2017 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.