Location: PHPKode > projects > Nitrotech > nitrotech/includes/user_functions_sessions.php
<?php

function check_sessions($page)
{
	global $SID, $table;

    $time = date("YdHi");

	@$query1 = "SELECT * FROM " . $table['sessions'] . " WHERE session_id = '" . $SID . "'";
    @$result1 = mysql_query($query1);
    @$time_stamp1 = mysql_result($result1,0,"time_stamp");
    if(!$time_stamp1)
    {
        //they have no sessions... better start one
		$query2 = "INSERT INTO " . $table['sessions'] . " VALUES ('', '$SID', '1', '$time', '$page', '0')";
        $result2 = mysql_query($query2);
	    if(!$result2)
	    {
	        message("Could not successfully run query ($query2) from DB: " . mysql_error(), 'die');
	    }
        unset($query2);
        unset($result2);
    }
    else
    {
    	//looks like they are logged in :)
        //time to check stuff
        if(($time - $time_stamp1) > 10)
        {
        	//looks like they have timed out
            //lets delete their old session and start a new one
	        $query2 = "DELETE FROM " . $table['sessions'] . " WHERE session_id = '" . $SID . "'";
	        $result2 = mysql_query($query2);
	        if(!$result2)
	        {
	            message("Could not successfully run query ($query2) from DB: " . mysql_error(), 'die');
	        }
	        unset($query2);
	        unset($result2);

            //new session
	        $query3 = "INSERT INTO " . $table['sessions'] . " VALUES ('', '$SID', '1', '$time', '$page', '0')";
	        $result3 = mysql_query($query3);
	        if(!$result3)
	        {
	            message("Could not successfully run query ($query3) from DB: " . mysql_error(), 'die');
	        }
	        unset($query3);
	        unset($result3);
        }
        else
        {
        	//they seem fine... better update them!
	        $query2 = "UPDATE " . $table['sessions'] . " SET page = '" . $page . "', time_stamp = '" . $time . "' WHERE session_id = '" . $SID . "'";
	        $result2 = mysql_query($query2);
	        if(!$result2)
	        {
	            message("Could not successfully run query ($query2) from DB: " . mysql_error(), 'die');
	        }
	        unset($query2);
	        unset($result2);
        }
    }
    unset($query1);
    unset($result1);

    //well thats their sessions done :)
    //no we need to see if anybody else has timed out so we can clear up the table

	@$query1 = "SELECT * FROM " . $table['sessions'];
    @$result1 = mysql_query($query1);

    if(!$result1)
    {
        message("Could not successfully run query ($query2) from DB: " . mysql_error(), 'die');
    }

    while($row1 =  mysql_fetch_assoc($result1))
    {
    	if(($time - $row1['time_stamp']) > 9)
        {
        	//looks like they have timed out... if they come back to the site they can start a new session logged out
            //lets get rid of them to clean up the table

	        $query2 = "DELETE FROM " . $table['sessions'] . " WHERE id = '" . $row1['id'] . "'";
	        $result2 = mysql_query($query2);
	        if(!$result2)
	        {
	            message("Could not successfully run query ($query2) from DB: " . mysql_error(), 'die');
	        }
	        unset($query2);
	        unset($result2);
        }
    }
    unset($query1);
    unset($result1);
}

//code borrowed and moddifed from phpBB
function get_sid($link)
{
	global $SID;

	if ( !empty($SID) && !preg_match('#sid=#', $link) )
	{
    	if(strpos($link, '?') == true)
        {
        	$link .= '&sid=' . $SID;
        }
        else
        {
        	$link .= '?sid=' . $SID;
        }
	}

	return $link;
}

function user_loggin($username, $password, $page)
{
	global $SID, $table;

	@$query1 = "SELECT * FROM " . $table['users'] . " WHERE username='" . $username . "' AND password='" . $password . "'";
    @$result1 = mysql_query($query1);
	@$user_id = mysql_result($result1,0,"id");
	@$active = mysql_result($result1,0,"active");
    if(!$user_id)
    {
        message("The username and password you entered did not match... Please go back and try again", 'message');
    }
    else
    {
    	if($active)
        {
	        $query2 = "UPDATE " . $table['sessions'] . " SET loggedin = '1', user_id = '" . $user_id . "' WHERE session_id = '" . $SID . "'";
	        $result2 = mysql_query($query2);
	        if(!$result2)
	        {
	            message("Could not successfully run query ($query2) from DB: " . mysql_error(), 'die');
	        }
	        else
	        {
	            message("Log in successful. Click <a href=\"index.php\">here</a> to return to home", 'message');
	        }
	        unset($query2);
	        unset($result2);
        }
        else
        {
        	message("Your account is not active", 'message');
        }
    }
    unset($query1);
    unset($result1);
    unset($user_id1);
}

function get_userinfo($data)
{
	global $SID, $root, $table;

	$query1 = "SELECT * FROM " . $table['sessions'] . " WHERE session_id = '" . $SID . "'";
    $result1 = mysql_query($query1);
    $user_id = mysql_result($result1,0,"user_id");
    if (!$result1)
    {
        message("Could not successfully run query ($query1) from DB: " . mysql_error(), 'die');
    }

	$query2 = "SELECT * FROM " . $table['users'] . " WHERE id = '" . $user_id . "'";
    $result2 = mysql_query($query2);
    if (!$result2)
    {
        message("Could not successfully run query ($query2) from DB: " . mysql_error(), 'die');
    }

	$userdata = mysql_fetch_object($result2);

    unset($query2);
    unset($result2);

	unset($query1);
    unset($result1);

    return stripslashes($userdata->$data);
}

function get_avatar()
{
	global $SID, $root, $table, $site_data;

	$avatar = get_userinfo('avatar');
    $avatar = explode(',', $avatar);

    if(!$avatar['0'])
    {
    	return;
        exit;
    }
    else
    {
        if($avatar['0'] == '2')
        {
        	return "<img src=\"" . $avatar['1'] . "\">";
        }
        else
        {
        	$avatar_data = explode(',', $site_data['avatar_info']);
        	return "<img src=\"" . $root.$avatar_data['3']."/".$avatar['1'] . "\">";
        }
    }
}

function veiw_userinfo($user_id, $data)
{
	global $SID, $root, $table;

	$query1 = "SELECT * FROM " . $table['users'] . " WHERE id = '" . $user_id . "'";
    $result1 = mysql_query($query1);
    if (!$result1)
    {
        message("Could not successfully run query ($query2) from DB: " . mysql_error(), 'die');
    }

	$userdata = mysql_fetch_object($result1);

	unset($query1);
    unset($result1);

    return stripslashes($userdata->$data);
}

function view_avatar($user_id)
{
	global $SID, $root, $table, $site_data;

	$avatar = view_userinfo($user_id, 'avatar');
    $avatar = explode(',', $avatar);

    if(!$avatar['0'])
    {
    	return;
        exit;
    }
    else
    {
        if($avatar['0'] == '2')
        {
        	return "<img src=\"" . $avatar['1'] . "\">";
        }
        else
        {
        	$avatar_data = explode(',', $site_data['avatar_info']);
        	return "<img src=\"" . $root.$avatar_data['3']."/".$avatar['1'] . "\">";
        }
    }
}

function loggedin()
{
	global $SID, $table;

	@$query1 = "SELECT * FROM " . $table['sessions'] . " WHERE session_id = '" . $SID . "'";
    @$result1 = mysql_query($query1);
    @$loggedin1 = mysql_result($result1,0,"loggedin");
    if(!$loggedin1)
    {
    	return '0';
    }
    else
    {
    	return '1';
    }
    unset($query1);
    unset($result1);
}

?>
Return current item: Nitrotech