genbodev2
6/16/2017 - 6:15 PM

Продлить сессию php из js

Продлить сессию php из js

// sessionExtension.php
session_start();
echo 'ok';
session_write_close();

// sessionExtension.js
$.post("/sessionExtension.php", function(result) {
            //debugger;
});

// --------------- полный скрипт js ---использует jqxWindow

/**
 * Created by Elovskiy Igor on 16.06.2017.
 * Allows you to extend the session. Shows a warning
 */

var sessionNotification = sessionNotification || {};

// Stores the modular window element
sessionNotification.window = null;

// Last Activity
sessionNotification.lastActivity = null;

// Session interval ID
sessionNotification.intervalId = null;

// Frequency of activity check (milliseconds)
sessionNotification.intervalTime = 60000;

// Session duration in minutes
sessionNotification.expirationMinutes = 60;

// Number of minutes before the warning
sessionNotification.warningMinutes = 5;

/**
 * It creates elements and session's data
 */
sessionNotification.create = function () {

    sessionNotification.initSession();
    sessionNotification.intervalId = setInterval(sessionNotification.checkWarningMinutes, sessionNotification.intervalTime);

};

/**
 * Init
 */
sessionNotification.initSession = function () {
    sessionNotification.lastActivity = new Date();
};

/**
 * The function will warn the user about the end of the session
 */
sessionNotification.checkWarningMinutes = function () {

    // Current
    var now = new Date();
    // The difference between the current time and the last activity time
    var diff = now - sessionNotification.lastActivity;
    // Convert in a minutes
    var diffMinutes = (diff / 1000 / 60);

    // It's time to warn the user
    if (diffMinutes >= sessionNotification.warningMinutes) {

        clearInterval(sessionNotification.intervalId);

        var h = now.getHours();
        if (h < 10) {
            h = '0' + h;
        }
        var m = now.getMinutes();
        if (m < 10) {
            m = '0' + m;
        }
        var d = now.getDate();
        if (d < 10) {
            d = '0' + d;
        }
        var mo = now.getMonth();
        if (mo < 10) {
            mo = '0' + mo;
        }

        sessionNotification.createDom(h + ':' + m + ' / ' + d + '.' + mo + '.' + now.getFullYear());

    }

};

/**
 * The extension of the session was ordered. Check if the time for ordering
 */
sessionNotification.checkExpirationMinutes = function () {

    // Current
    var now = new Date();
    // The difference between the current time and the last activity time
    var diff = now - sessionNotification.lastActivity;
    // Convert in a minutes
    var diffMinutes = (diff / 1000 / 60);

    // Order Time Out
    if (diffMinutes > sessionNotification.expirationMinutes) {
        sessionNotification.sessionLogout();
    } else {
        sessionNotification.initSession();
        sessionNotification.intervalId = setInterval(sessionNotification.checkWarningMinutes, sessionNotification.intervalTime);
        $.post("/sessionExtension.php", function (result) {
            //debugger;
        });
    }
};

/**
 * Create DOM
 */
sessionNotification.createDom = function (actualTime) {

    if (sessionNotification.window) {
        $(sessionNotification.window).remove();
    }

    var jqxWindow = document.createElement('div');

    $(jqxWindow).attr('id', 'jqxWindowSessionNotification');

    var header = document.createElement('div');
    header.textContent = core.Tr('Warning');
    jqxWindow.appendChild(header);

    var content = document.createElement('div');
    debugger;
    var textDiv = document.createElement('div');
    content.appendChild(textDiv);
    textDiv.innerHTML = core.Tr('Notification from') + ' ' + actualTime + '<br />' +
        core.Tr('Your session expires in') + ' ' +
        (sessionNotification.expirationMinutes - sessionNotification.warningMinutes) + ' ' + core.Tr('Minutes');

    var buttonWrapper = document.createElement('div');
    buttonWrapper.style.cssText = 'display: flex; margin-top: 20px; justify-content: center;';

    var okButton = document.createElement('input');
    okButton.setAttribute('type', 'button');
    okButton.setAttribute('value', core.Tr('Extend'));
    okButton.style.cssText = 'margin-right: 10px;';

    var cancelButton = document.createElement('input');
    cancelButton.setAttribute('type', 'button');
    cancelButton.setAttribute('value', core.Tr('Log out'));

    buttonWrapper.appendChild(okButton);
    buttonWrapper.appendChild(cancelButton);
    content.appendChild(buttonWrapper);

    jqxWindow.appendChild(content);

    document.body.appendChild(jqxWindow);

    sessionNotification.window = jqxWindow;

    $(jqxWindow).jqxWindow({
        position: 'center',
        height: 115,
        width: 270,
        theme: 'metro',
        isModal: true,
        showCloseButton: false,
        resizable: false,
        okButton: $(okButton),
        cancelButton: $(cancelButton),
        initContent: function () {
            $(okButton).focus();
        }

    });

    // Remove elements
    $(jqxWindow).on('close', function (event) {
        if (sessionNotification.window) {
            $(sessionNotification.window).remove();
            $('.jqx-window-modal').remove();
        }
        if (event.args.dialogResult.OK) {
            sessionNotification.checkExpirationMinutes();
        } else if (event.args.dialogResult.Cancel) {
            sessionNotification.sessionLogout();
        } else {
            // X
        }
    });
};

/**
 * Logout
 * @returns {*}
 */
sessionNotification.sessionLogout = function () {
    return logout();
};

// Run
sessionNotification.create();