Working with Session

Advanced Security - PHP Register/Login System Advanced Security - PHP Register/Login System / How-to Last updated on Updated  Jan 16, 2019

ASSession Class

By including ASEngine/AS.php file into some of your own files, your session will be automatically started. However, if you want to do it by your self, you need to call startSession() method from ASSession class.

ASSession::startSession();

Note! Always start session using this function because it will start secure session!

If you want to destroy existing session, just call destroySession() method:

ASSession::destroySession();

In order to store something to the session, you can use set method

ASSession::set("something", 5);

And if you want to get something from the session, for example user ID, call get method

$userId = ASSession::get("user_id");

If you want to unset some session item, you can do it by calling destroy method

ASSession::destroy('someething_that_was_set_before');

Session Lifetime

By default, AS session cookie will use lifetime configuration from your php.ini file, which usually means that session cookie will expire right after you close the browser window. In order to modify that, just edit ASSession class and inside startSession function replace $cookieParams["lifetime"] with an integer that represent lifetime of the session cookie in seconds. For example, if you want your cookie to expire after 2 hours, your startSession method should look like following:

public static function startSession()
{
    //...
    session_set_cookie_params(
        7200,
        $cookieParams["path"],
        $cookieParams["domain"],
        SESSION_SECURE,
        SESSION_HTTP_ONLY
    );
    //...
}

In order to allow auth cookie to be shared across multiple subdomains, just go to ASEngine/ASSession.php, and replace $cookieParams["domain"] with .domain.com inside startSession method, and you are good to go.