<?PHP
function generate_user_id()
{
// this value is needed so nobody could steal a session from another use
// putting in here the client IP would be best, but in fact ISPs like AOL
// use their proxy and so client requests come from different IPs, even
// if the same client requests stuff and worst of it, this might change
// within seconds.
// so yet we only check the HTTP_USER_AGENT, maybe someday there comes be more.
return md5($_SERVER['HTTP_USER_AGENT']);
}
function kill_user_session()
{
// empty the session array by filling it with an empty array
$_SESSION = array();
// use the function coming with PHP to destroy the session
@session_destroy();
return 0;
}
function user_session_start()
{
session_start();
$session_id = session_id();
if(!session_is_registered('last_action'))
$_SESSION['last_action']=time();
if(!session_is_registered('session_unique_id'))
$_SESSION['session_unique_id'] = md5(uniqid(rand(), true));
//if(!session_is_registered('session_unique_id'))
// $_SESSION['session_unique_id'] = generate_user_id();
return $session_id;
}
function user_session_store( $userdata, $cookie_name = "gframe" )
{
// hash password so nobody can steal the password out of the local cookie file
$password = md5($password);
// put the cookie together, serialize the array so it can be handeled and make a simple encryption around it
$cookie['username'] = $userdata['username'];
$cookie['password'] = $userdata['password'];
$cookie = serialize($cookie);
$cookie = base64_encode($cookie);
setcookie("$cookie_name", $cookie, time()+86400);
}
function user_session_restore( $cookie_name = "gframe" )
{
$cookie = $_COOKIE[$cookie_name];
$cookie = unserialize(base64_decode($cookie));
$userdata['username'] = $cookie['username'];
$userdata['password'] = $cookie['password'];
return $userdata;
}
function check_session($session_expire_time)
{
if($_SESSION['session_unique_id'] != generate_user_id())
$session = kill_user_session();
if(($_SESSION['last_action'] + $session_expire_time) < time())
$session = kill_user_session();
else
{
$_SESSION['last_action']=time();
$session = true;
}
return $session;
}
function check_user_session($session_expire_time)
{
// function get its parent function from check_session
$result = check_session($session_expire_time);
// check if the session is a 'admin session'
if($_SESSION['session_type'] != 'user')
$result = false;
return $result;
}
function check_admin_session($session_expire_time)
{
// function get its parent function from check_session
$result = check_session($session_expire_time);
// check if the session is a 'admin session'
if($_SESSION['session_type'] != 'admin')
$result = false;
return $result;
}
?>