1

I try to add 'site gate' to http://bartenderlab.it. It's alcohol content so people under 18 are not allowed to view this site.

I created module with hook_init:

function gate_init() {

    $curr_uri = request_uri():
    if (strpos($curr_uri, '/gate') === FALSE) {

        if (! isset($_SESSION[GATE_SESSION_NAME])) {

            if (! isset($_COOKIE[GATE_COOKIE_NAME])) {

                    if(strlen($curr_uri) > 1) {

                        drupal_goto('gate', array('destination' => substr($curr_uri, 1)));

                    }
                    drupal_goto('gate');    

            } else {

                $_SESSION[GATE_SESSION_NAME] = TRUE;

            }
        } 
    } else if(isset($_SESSION[GATE_SESSION_NAME])) {

        drupal_goto('<front>');

    } 
}

Under /gate url is form with:

function gate_form_submit($form_id, &$form_state) {

    $post = $form_state['values'];
    $day = intval($post['gate_day']);
    $month = intval($post['gate_month']); 
    $year = intval($post['gate_year']);
    $isRemember = $post['gate_remember_me'];
    if($day && $month && $year) {

        $allowed_age = (int) variable_get('gate_minimal_age', 18);
        $min_born_time = mktime(0, 0, 0, date("m"), date("d"), date("Y") - $allowed_age);
        $born_time = mktime(0, 0, 0, $month, $day, $year);

        if($min_born_time >= $born_time) {

            $_SESSION[GATE_SESSION_NAME] = TRUE;
            if($isRemember) {

                setcookie(GATE_COOKIE_NAME, sha1($born_time + srand()), time() + 60 * 60 * 24 * 7, '/');

            } 
            $dst = '<front>';

            if(isset($_GET['destination'])) {

                $dst = $_GET['destination'];

            }

            drupal_goto($dst);
        }
    } 
}

If checkbox for $isRemember is not checked user should only view site until browser close.

so I set:

ini_set('session.cookie_lifetime',  0);

in settings.php

But after restart browser gate page doesn't appear.

Also after user logout I am redirect to gate page instead of front page.

Please help.

2 Answers 2

3

Have a look at Disclaimer module; it seems to be the functionality you're looking for.

0
0

The nice code I have which work is:

function gate_init() {
    global $user;

    $curr_uri = request_uri(); 
    $request_type = substr($_SERVER['HTTP_ACCEPT'], 0, strpos($_SERVER['HTTP_ACCEPT'], ','));

    if($request_type == 'text/html') {

        // check if the user is going to logout and have gate enabled
        if($curr_uri == '/' && $user->session == '' && isset($_COOKIE['keep_logged_in']) && $_COOKIE['keep_logged_in'] == 1) {
            _gate_allow_entrance();
            setcookie('keep_logged_in', '', time() - 42000, '/');
        }

        // maybe they have cookie set before
        if(isset($_COOKIE[GATE_COOKIE_NAME]) && $_COOKIE[GATE_COOKIE_NAME] == TRUE && !isset($_SESSION[GATE_SESSION_NAME]))
            _gate_allow_entrance();

        if($curr_uri != GATE_URL) {
            if(!(isset($_SESSION[GATE_SESSION_NAME]) && $_SESSION[GATE_SESSION_NAME] === TRUE))
                drupal_goto('gate');
        }

        // if there is a gate key and logout was called, then keep the key within next request
        if($curr_uri == '/logout' && isset($_SESSION[GATE_SESSION_NAME]) && $_SESSION[GATE_SESSION_NAME] === TRUE)
            setcookie('keep_logged_in', 1, time() + 10, '/'); // only for 10 seconds

    }

    return true;
}

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.