login and logout session

how to use sc_alert,sc_confirm() macros in library and include them in application.

how to maintain session_id in database and check in every application if same session_id ->continue application else show alert message and logout how to do this.
i tried using library and called a function
my code is
function check_session()
{
$check_active_user=[usrcheck_login];
$check_ssn_id=session_id();
sc_lookup(sn_data,“select sc_session from sec_logged where login=’$check_active_user’”);
if(!empty({sn_data}))
{
$datainsession={sn_data[0][0]};
}
else
{
$datainsession=’’;
}
if($check_ssn_id!=$datainsession)
{
sc_alert(‘Session expired’);
sc_redir(‘login’);
}
}

i called this function in all applications to check session but it is not working.