0

I created a custom module which has the following hook_init() implementation.

function gate_init() {
  $curr_uri = check_plain(request_uri());
  if (strpos($curr_uri, 'admin') === FALSE) {
    if (strcmp($curr_uri, '/gate') != 0) {  
      if (!sess_read(GATE_SESSION_NAME)) {
        if (!isset($_COOKIE[GATE_COOKIE_NAME])) {
          drupal_goto('gate');
        }
        else {
          sess_write(GATE_SESSION_NAME, TRUE);
        }
      }
    }
    else {
      if (sess_read(GATE_SESSION_NAME) || isset($_COOKIE[GATE_COOKIE_NAME])) {
        if (!sess_read(GATE_SESSION_NAME)) {
          sess_write(GATE_SESSION_NAME, TRUE);
        }
        drupal_goto('/');
      }
    }
  } 
}

It's crazy, but this code causes login problems for authenticated users. I cannot login, I see no errors, I just cannot login if I provide correct username and password.

If I comment out these lines, I can login.

if (!sess_read(GATE_SESSION_NAME)) {
  if (!isset($_COOKIE[GATE_COOKIE_NAME])) {
    drupal_goto('gate');
  }
  else {
    sess_write(GATE_SESSION_NAME, TRUE);
  }
}

I check further and if (!sess_read(GATE_SESSION_NAME)) is the part causing login problems. Could anybody give me an advice?

3
  • Hello @user1178, and welcome on Drupal Answers. It's not the cause of the issue you are having, but you should not use check_plain() on a value that you are comparing with a string; check_plain() should be used for values that you output, for example for a string you get as user input, and that could contain hHTML tags when you want to use as plain text. An example could be usernames like "<a>ntares" or "<b>art."
    – apaderno
    May 4, 2011 at 16:57
  • 1
    I am not if it is the reason for your problems, but try to use $_SESSION instead of sess_read/write() like you're doing currently, it is possible that this causes issues with Drupal's session handling.
    – Berdir
    May 4, 2011 at 17:26
  • Berdir gave the correct answer. sess_read/sess_write() are private Drupal functions. Don't use them. To store something in the session, use $_SESSION. May 4, 2011 at 17:43

1 Answer 1

2

sess_read() should not be used in a module, as that function is passed to session_set_save_handler() from _drupal_bootstrap(), which is the function that is called during Drupal bootstrap.
The purpose of the function is not to read values from the PHP session; if you need to do that, the you can just read the content from $_SESSION[].
To notice that session_start() is called from Drupal during its bootstrap; that means that after a full bootstrap (or any bootstrap that goes past the DRUPAL_BOOTSTRAP_SESSION phase) a session is initialized, and $_SESSION[] can be used.

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.