hook_init() causes login problems 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?
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.